Promise or async/await how todo please?

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

}

Hi Mauro,

Is the code that you posted from a backend file? If so, that explains why values are undefined. The wix-users and wix-storage APIs you are importing are unavailable in the server-side (backend) code and are meant to be used by client-side code. You could get the values needed and then pass them as parameters to web modules in the backend. See the article Calling Server-side Code from the Front-end with Web Modules for more information on using backend code.

Good luck,

Yisrael

Hi Yisrael as I wrote it is in public/userVerify.js, not in backend.
The problem is that when in main code, inside home page (eg), the istructions, that follow the call to userVerify(), run before that the same function userVerify ends, they are not able to get the right values processed by function.
I need that routine can pause until userVerify ends, for this reason I would be able to manage correctly Promises and async/await

Dear Yisrael, the beginners in Wix Code need some correct examples to became introduced in the knowledge of Javascript. I saw that in Wix guide and examples you take for granted the concept of Promises and Async/await is so simple. Evidently it doesn’t. I believe that these concepts are so in the centre of the programmation that they could deserv more attention and more deeppening.

Here something of good:

thx
Mauro

Hi Mauro,

We at Wix certainly don’t take anything for granted. You can find a couple of articles in the Wix Code Resources:

  • Working with Promises in Wix Code

  • Promises, Promises

  • (and thanks for the link you provided above)
    However, users who intend to work with code in the product, and not just the features in the user interface, will need to learn basic coding concepts to accomplish what they want. There are a wealth of Javascript coding sites which providing help learning Javascript from basic to advanced - Javascript.info is a good one. The Wix Code Resources page provides tutorials, examples, and articles on getting the most out of Wix Code. Google and the other search engines are your friends. And of course, as questions or difficulties arise, we are here to help.

Have fun,

Yisrael

Hi Yisrael, thx for your feedback and patient. I’m following you advices and I enrolled different javascript courses on internet. It’s a world, a wonderful world :slight_smile:
And Wix is a great big idea !

I’ve been coding for 40 years and I don’t find the examples strong enough. I feel that as a customer I should be allowed to say that…

This is an old post and is being closed. If you have further questions please open up a new thread with your own problem/issue instead of bumping up old posts.

Bumping up old post is against the Community Guidelines .