Help with async code please

Hi,

I think my understanding of async code and the scope of variables is not quite right.
Please take a look at the code below:


$w.onReady(function () {
// Get thread ID from URL
memberIDString = wixUsers.currentUser.id;
$w(“#threadsDataset”).onReady( () => {
threadIDString = wixLocation.url.substr(wixLocation.url.indexOf(“/createpost/”) + 12);

	// Get all posts from the database which match the current thread, for which currentUser is in the access list 
	wixData.query("posts").eq("threadID",threadIDString) 
						.contains("accesslist",memberIDString) 
						.find() 
						.then( (results) => { 
							let numItems = results.totalCount; 
							if (numItems === 0) return; 
							postOwner = results.items[0].owner; 
							// Get the owner of first post from dB and extract their picture, firstname, lastname 
							wixData.query("members").eq("ID", postOwner) 
								.find() 
								.then( (posterResults) => { 
									postOwnerIcon = posterResults.items[0].profilePic; 
									postOwnerFirstname = posterResults.items[0].firstname; 
									postOwnerFirstname = posterResults.items[0].lastname; 
								}) 
								.catch((err) => { 
									console.log(err); 
								}); 

Stepping through the code with the debugger, memberIDString and threadIDString are correctly set initially.

Later on, in the “.then( (results) => {” section, memberIDString and threadIDString are both “undefined”.

I thought that by declaring these variables at the top of the file, they would be global, but I guess I don’t understand scoping and async code? Or is there some other problem?

Please help if you have any suggestions or knowledge… I’d really appreciate it, as I am stalled on this

1 Like

Hi Mike,
Unless I’m missing something, these variables should be accessible, it’s hard to know what happens without debugging the original code. You can try saving it and posting the url here and we can try to help.
Note that we are also allowing a newer JS syntax called “async await”, that allows you to write asynchronous code (promises) using a synchronous-like syntax. I converted your code to demonstrate:

$w.onReady(function () {
	// Get thread ID from URL
	memberIDString = wixUsers.currentUser.id;
	$w("#threadsDataset").onReady(async () => {
  		threadIDString = wixLocation.url.substr(wixLocation.url.indexOf("/createpost/") + 12);
	
		// Get all posts from the database which match the current thread, for which currentUser is in the access list
		const postsResults = await wixData
			.query("posts")
			.eq("threadID",threadIDString)
			.contains("accesslist",memberIDString)
			.find();

		let numItems = results.totalCount;
		if (numItems === 0) return;
		postOwner = results.items[0].owner;
		// Get the owner of first post from dB and extract their picture, firstname, lastname
		const posterResults = await wixData
			.query("members")
			.eq("ID", postOwner)
			.find();
			
		postOwnerIcon = posterResults.items[0].profilePic;
		postOwnerFirstname = posterResults.items[0].firstname;
		postOwnerFirstname = posterResults.items[0].lastname;
	});
});

Hi Mike/Tomer,
I tried async/await on several functions and works great. However, it doesnt work in the onReady function. Should I do something different for the OnReady function than the one below
$w.onReady( async function () { …
Yes, I add the “await” before my wixData.query calls.

If not, is there a different way to use the async/await for the onReady function?

Please help !!!

Hi Vidya,
The function you pass to “onReady” is just like any other function, async await should work.
Can you post the full code so we can find the issue? Do you get any errors in the developer console?

Hi Tomer,
HEre goes a screenshot of my code. I am highlighting the way I am calling async/await. I get the error message for await as “await is undefined”

This code is inside another onReady callback, which doesn’t have the async keyword.

Didnt realize that, thanks Tomer. It worked.

1 Like