returned variable is undefined

I’m having a problem that’s driving me crazy

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);

$w.onReady(async function() { // from my page code
hasAccess = checkAccess ( );
console.log("* hasAccess check : ", hasAccess) = * hasAccess check : undefined

Can anyone tell me how to send/receive the yes/no value to my page???

Hey, I think you’re missing “await” before calling “check Access”. Give it a try and update :slight_smile:

1 Like

Hi Tomer,

Good day,
I hope your doing well.
I have a question I hope you can help me with it.
Please see the URL —> https://www.wix.com/code/home/forum/questions-answers/create-two-data-row-in-one-datacollection-by-one-owner-id

1 Like

Hello Tomer,

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 again.

Hi,
I hope this thread will be useful.
Good luck

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

} // end function

Hi again,
Can you please share a link to your site?
Roi

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.

Appreciate you taking the time to look at this.

Hi again,
Can you please specify the name of the page ?
Roi

Hello Roy,

I’ve changed my code so that I’m using session data to pass the values so my code no longer will show this error. Thanks for taking an interest.