From my page onReady I’m invoking a function defined in my public lib.js
The function returns a variable that is set to either yes or no
per my console.log the variable is correctly set in the function,
but, per my console.log, my page onReady function is receiving an undefined variable:
export async function checkAccess // defined in lib.js
let hasAccess = “no”;
…check database for access
if {access record found
hasAccess = “yes”}
console.log("*** found current access : ", hasAccess) = *** found current access : yes
return (hasAccess);
The await isn’t helping. ( I do think there is definitely something wrong with my async processing, but I don’t believe it’s causing the undefined problem.) Anyway I’ve worked around the undefined return by using session data to store my results, not a very elegant solution but I’m desperate to get this working.
Thanks for your input, I’ve been feeling abandoned the last few weeks. It’s nice to know someone reads these cries for help.
Do you have any code examples of async processing that includes some forEachItem procesing in the middle of the flow? I’m running into a problem where I get dropped out of async processing when I’m doing collection queries in the forEach looping. It would be nice to have a example to look at.
Thanks Roi, it’s a nice example and very clear.
But in my code I’m doing queries within the forEach loop and I’m dropping out of async processing. my code is below ( I’m async up to the forEach loop, but once I’m in the activitiesSearch function processing I lose that. )
export async function checkUserProfile(userEmail) {
bunch of code…then
await (checkActivityType(userEmail));
return;
} // end function
export async function checkActivityType(userEmail) {
await wixData.query(“userActivity”) // find past and current activities assigned to the user
.eq(“email”, userEmail)
.find()
.then(async (uAresults) => {
await uAresults.items.forEach(async (thisActivity) => {
await activitiesSearch(thisActivity, todayDateYYYYMMDD); // call function activitiesSearch
}); // end for each item loop then
return;
}) // end userActivity then
.catch( (error) => {
console.log("*** checkActivyType error ", error, ", for: ", userEmail);
return;
}); // end catch error and userActivity query
} // end function
async function activitiesSearch (thisActivity, todayDateYYYYMMDD) {
if ((thisActivity.activityStartDateYYYYMMDD > todayDateYYYYMMDD) || (thisActivityactivityEndDateYYYYMMDD <= todayDateYYYYMMDD)) {
return; // activity is not current, exit function
}
code to store some session data…
await wixData.query(“activities”)
.contains(“title”, thisActivity.activityName)
.find()
.then(async (Aresults) => {
let itemsActivity = Aresults.items;
let itemActivity = itemsActivity[0];
let activityProgram = itemActivity.activityProgram;
return activityProgram;
}) // end activities then
.then(async (activityProgram) => {
await checkActivityTitles(activityProgram); // call function checkActivityTitles
return;
}) // end checkActivityTitles then
.catch(async (error) => {
thisActivityFound = “no”;
console.log("*** activitiesSearch error : ", thisActivityFound, " : ", error);
return (thisActivityFound);
}); // end catch error and queries
OK, but warning - my site is under construction, I’m constantly making changes. Trying to force it into working. Also in the past hour I think I’ve found a problem with one of my collections that may be contributing to my errors.
My site is www.aesresourcesite.com, but you need a valid userid to get into the pages that aren’t working.
Making progress!!! I took out some of the return statements I had and it looks like I got my async back.
Still testing but it’s looking better.