Hi guys, I’m trying to figure out if the currentUser has already purchased a specific formula or not when clicking on my #button1 element. If yes, I will consider this user as a FullAccess user (with specific authorisation).
To do that, I have 2 collections : “Achat” (which means “Purchase”) and Formule (which means “Formula”).
From the button1 onClick event, I call the “isUserFullAccess” function and wait for the result to continue. In the isUserFullAccess function, I define a variable “Answer” which will be my returning function value and set it to false by default. Then I will define and execute a first query that will store all the results in an array (myarray[]). Then I will go through each element of this table and define and execute another query to test the value of the information I’m looking for ( NumberOfModules). If the value is “All”, then I would like to set my variable “Answer” to true and at the end returning this value.
The problem I have is that i can see the “All” value found and diplayed in the console but the returning value of the isUserFullAccess function is always “False”. Can someone has an idea of what happen ?
$item("#button1").onClick(async (event, $item4) => {
//Add your code for this event here
let $user = wixUsers.currentUser;
let $userId = $user.id;
let $role = $user.role;
let $isLoggedIn = $user.loggedIn;
let $isFullAccessUser = await isUserFullAccess($userId);
console.log($isFullAccessUser);
});
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;
console.log(errorMsg);
} );
myarray.forEach(async function(item, index, array) {
console.log("Id en cours dans le tableau : " + item);
await wixData.query("Formules")
.eq("_id", item)
.find()
.then( (results) => {
if(results.items.length > 0) {
let currentFormule = results.items[0];
let NumberOfModules = currentFormule.numberOfModules;
console.log("Nombre de modules : " + NumberOfModules);
if (NumberOfModules === "All"){
console.log("Yes, All found");
answer = true;
}
}
} )
.catch( (err) => {
let errorMsg = err;
console.log(errorMsg);
} );
});
return answer;
}