I finally solved my problem by using a do…while() to go through the array instead of a forEach ( async function…). I guess the answer variable of this function is not related to the answer variable of the overall function …
Here is the new code that works:
async function isUserFullAccess(userId){
let myarray = [];
let answer = false;
let queryAchatforMember = wixData.query("Achat")
.eq("member", userId);
let queryAchatModule = wixData.query("Achat")
.eq("isModules", true);
let queryAchatModuleMember = queryAchatforMember.and(queryAchatModule);
await queryAchatModuleMember.find().then( (results) => {
if(results.items.length > 0) {
let i = 0;
do {
let currentAchat = results.items[i];
let currentAchatId = currentAchat._id;
let currentAchatFormuleId = currentAchat.formule;
myarray.push(currentAchatFormuleId);
i = i + 1;
} while (i < results.items.length);
}
})
.catch((err) => {
let errorMsg = err;
} );
if (myarray.length > 0){
let i = 0;
do {
let formuleId = myarray[i];
await wixData.query("Formules")
.eq("_id", formuleId)
.find()
.then( (results) => {
if(results.items.length > 0) {
let currentFormule = results.items[0];
let NumberOfModules = currentFormule.numberOfModules;
if (NumberOfModules === "All"){
answer = true;
}
}
} )
.catch( (err) => {
let errorMsg = err;
} );
i = i + 1;
} while (i < myarray.length);
}
return answer;
}