Help with undefined values.

Hi Folks, Anyone know why I’m getting an ID but all my other fields are undefined? I’ve commented out the insert for now so I’m just going by the console logs. Name, loginemail and mainPhone are all undefined.

import wixUsers from ‘wix-users’ ;
let user = wixUsers.currentUser;
let userId = user.id;
let name = user.name;
let loginEmail = user.loginEmail;
let mainPhone = user.mainPhone;

$w.onReady( function () {
$w( “#learnerData” ).onReady(() => {

    wixData.query( "Learners" ) 
        .eq( "_id" , userId) 
        .find() 
        .then((results) => { 

if (results.items.length > 0 ) {
} else {
console.log(userId)
console.log(name)
console.log(loginEmail)
console.log(mainPhone)
// wixData.insert(‘Learners’, { userId, name, loginEmail, mainPhone });
}
})
. catch ((err) => {
let errorMsg = err;
});
})
})

Hello.

To get user email, you need to use getEmail function.

As for the name, you need to query Members/PrivateMembersData collection with user ID and get the names as shown in sample code below:

wixData.query("Members/PrivateMembersData")
        .eq("_id", wixUsers.currentUser.id)
        .find()
        .then((results) => {
         console.log(results.items[0].firstName);
        })

Please note that these code should be inside the if but not else statement.

As for the phone number, this is not a built-in field in the Members/PrivateMembersData collection.

Assuming you have such a field in the learners’ collection You will have to access it using:

console.log(results.items[0].mainPhone);

Good luck!

thanks for the help!