I would like an help on the following routine please.
It’s a function.js in public that the main routine call in order to verify a logged member, compare its email with email in db Soci, and set a variable Level, from 1 to 3, with a local.setItem(“level”), in order to reutilize them in main routine to wixlocationTo different dynamic pages.
My problem is this: when I call this function, the main process go straight, it doesn’t wait the completation of this function, so some variables go in ‘undefined’.
How I can put this routine in correct form of Promise and/or async/await form ?
Now I’m learning these, they are my first steps
thx in advance
mauro
public/userVerify.js
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import {local} from ‘wix-storage’;
import {stringToDate} from ‘public/stringToDate.js’;
export function userVerify() {
let oggi = new Date();
let user = wixUsers.currentUser;
user.getEmail()
.then((email) => {
let userEmail = email;
return userEmail;
})
.then(function (userEmail) {
wixData.query("Soci")
.contains("email", userEmail)
.find()
.then((results) => {
if (results.items[0] !== null && results.items.length > 1) {
let Nsoci = results.items.length;
let i = 0;
var iSup = 0; // questo l'indice che ci darà il Socio più recente
let scadenza = new Array();
for (i = 0; i < results.items.length; i++) {
scadenza[i] = results.items[i].scadenza.getTime();
if (scadenza[i] > scadenza[i - 1]) {
iSup === i;
}
}
}
else {iSup = 0; }
if (results.items[0] !== null && results.items.length > 0) {
let userName = results.items[0].nome; let upName = userName.substring(0, 1);
let lowName = userName.substring(1,userName.length);
userName = upName.toUpperCase() + lowName.toLowerCase(); local.setItem("userName", userName); //
let datascadenza = results.items[iSup].scadenza;
let tessera = stringToDate(datascadenza);
let tes = tessera.getTime();
if (tessera < oggi.getTime()) {
local.setItem("level", "2,5");
console.log('userVerify - La tua tessera è scaduta!' + 'tessera: ' + tes + " oggi: " + oggi.getTime());
if (tessera < (oggi.getTime()-5184000000) ) local.setItem("level","2");
} else {
local.setItem("level", "3"); }
} else {local.setItem("level", "1");}
});
})
.catch((err) => {console.log(err);});
}