Queried value, Variable, DisplayorNot, Insert

Hi!,

After querying a database, and displaying the result (loginEmail) in a textbox (#text73), I can use it in an insert (//COMMENTLINE2). No problems with that. I paste the code below

But in case I would not save the loginEmail in the textbox, I could not find the way how to do the insert of that particular value. As it is in a variable (userEmail) (//COMMENTLINE1), I assumed I could use it in the Insert, but I couldn’t. I tried using let and var, thinking it was an scope issue… but got stuck . Looks like the moon to me.

Some help would be much appreciated

THE CODE
import wixUsers from ‘wix-users’ ;
import {session} from ‘wix-storage’ ;
import wixData from ‘wix-data’ ;
import wixLocation from ‘wix-location’ ;

$w.onReady( function () {
wixData.query( “Members/PrivateMembersData” )
.eq( “_id” , wixUsers.currentUser.id)
.find()
.then( (results) => {
let userEmail = results.items[ 0 ].loginEmail; //COMMENTLINE1
let userId = results.items[ 0 ]._id;
$w( “#text73” ).text = userEmail;
});

$w( "#repeater1" ).onItemReady(($w, dataItem, index) => { 

let linkToDynamicPage = dataItem[ “link-clubbitnodo-1-title” ];
let linkToDynamicPage1 = dataItem[ “link-clubbitnodo-2-title” ];

    $w( '#box72' ).onClick(() => { 

//if (circuito === 1) {

$w( ‘#dataset1’ ).getCurrentItem();

let toInsert = {
“title” : $w( ‘#dataset1’ ).getCurrentItem().title,
“nombre” : $w( ‘#dataset1’ ).getCurrentItem().nombre,
“origen” : “1” ,
“circuito” : “1” ,
“userMail” : $w( ‘#text73’ ).text, // COMMENTLINE2

};

wixData.insert( "clubbitUsrccio" , toInsert)  
.then( (results) => { 
    console.log( "done adding item" ) 
}) 
. **catch** ( (err) => { 

let errorMsg = err;
});

}); 

});

});

Instead of

"userMail":     $w('#text73').text,  

try

"userMail":    userEmail

Note that the last comma is gone!

Thanks for replying !
Unfortunately I had tried that, and the code shows me an error…

“userEmail”: is not defined,


let toInsert = {
 "title":        $w('#dataset1').getCurrentItem().title, 
 "nombre":       $w('#dataset1').getCurrentItem().nombre,
 "origen":      "1", 
 "circuito":    "1", 
 "userMail":     userEmail   // COMMENTLINE2

};

Ah, I get it. Do this. Move the let userEmail… to a position above the $w.onReady, like so:

let userEmail;
$w.onReady(function () 

then change this line:

let userEmail = results.items[0].loginEmail;    //COMMENTLINE1

into

userEmail = results.items[0].loginEmail;    //COMMENTLINE1

and then in the onClick it will work.

It worked!!
Thank you very very much for your time

Just to clarify though…the variable was defined inside the onready, so in order to solve the problem, I had to define it before the onready This way it has already been created at the time that everything happens (sorry for my lack of technicisism)…something like that ??

It´s about “scope”. In your first solution, the declaration was inside the $w.onReady. But this one is isolated from the .onClick, so unknown. If you move it up, it is still part of your code, but now you have made the var global instead of local.