Setting database field with user email

Hi,
Spent hours on this and can’t work it out - pretty new to coding!
I’m trying to get the user email address of a member and then save that as a field in another database (‘#picksData’). I have used .setFieldValue() function as per below but for some reason the database does not update with the email address.
I’ve tried moving around where the .setfielfvalue() line is but then it says the ‘email’ variable isn’t defined when i move it outside the .then().
The bit where it saves ‘weekNo’ works, so i dont understand what i’m doing wrong?
#setFieldValue, #getEmail()


import wixUsers from ‘wix-users’;

$w.onReady( () => {
$w(“#picksData”).onBeforeSave( () => {
let filterWeekNo = $w(“#filtersData”).getCurrentItem().weekNo;

let user = wixUsers.currentUser;
let userId = user.id;
let isLoggedIn = user.loggedIn;
console.log(“gotuser”);

user.getEmail() 
    .then( (email) => { 

let userEmail = email;
$w(“#picksData”).setFieldValue(“userEmail”,email);
$w(“#picksData”).save();
} );

$w("#picksData").setFieldValue("weekNo",filterWeekNo); 
$w("#picksData").save(); 
console.log("Continuing save"); 

} );

});

Try calling a function with your onReady at the start

$w.onReady(function () {
$w("#picksData").onBeforeSave(() => { 

Also combine the two setFieldValue into just the one so that you only have the one save afterwards.

Examples

Set several fields’ values

$w("#myDataset").setFieldValues( {
  "title":  "New Title",
  "name":   "New Name"
} );

See here for more info:

Thanks for the suggestions, the set several fields will be useful and I’ll put that in. I was just trying to break it down to see why the weekNo was updating in the database but the userEmail wasnt.

I added the function as you described but it still didn’t work.
so i put in this: console.log(“got user, now getting email”);
before this: user.getEmail()
just so i could see where it breaks on the getEmail section

when i run it in preview mode the console doesnt ever seem to perform this part:

user.getEmail()
.then( (email) => {
let userEmail = email;
console.text(userEmail)
$w(“#picksData”).setFieldValue(“userEmail”,userEmail);
console.log(userEmail)
$w(“#picksData”).save();
} );

the next .setValue performed is the weekNo one?
Is there a reason it is skipping that part?

Can anyone help?

Did you find a solution why it is not saved? I have the same problem!

I’m having the same issue. Any help would be great!

What issue are you having. Please repost your question in a new post so more people will see it. Clearly explain what you are trying to do and what you want to accomplish.