Capturing user full name to global variable

Hello, I am using currentMember.getMember to get data on the currently logged in user.

It captures the data correctly and displays the data correctly in the function but it will not allow me to use this outside of it.

Thoughts?

import { currentMember } from ‘wix-members-frontend’;

var FullName = “Hello world”

currentMember.getMember()
.then(async(member) => {
const id = member._id;
let FullName = await ${member.contactDetails.firstName} ${member.contactDetails.lastName};
console.log(id) //This works
console.log(FullName) //This works
return member;
})
.catch((error) => {
console.error(error);
});

$w.onReady(function () {

console.log(FullName) // Returns ‘HELLO WORLD’ vs results from getMember()

});

This is because currentMember.getMember() needs to be called at some point and only then will it run and fetch the member’s name.

Calling it within the onReady function runs the code when the page is loaded.

So when you log the member name, it will return the default value which in this case is Hello world since the currentMember.getMember() is not being called.

If you want the code to be outside of the onReady function, you can write it under another function, something like:

import { currentMember } from ‘wix-members-frontend’;

var FullName = “Hello world”

function gemember () {

currentMember.getMember()
.then(async(member) => {
const id = member._id;
FullName = await ${member.contactDetails.firstName} ${member.contactDetails.lastName};
console.log(id) //This works
console.log(FullName) //This works
return member;
})
.catch((error) => {
console.error(error);
});

}

$w.onReady(function () {

getMember();
console.log(FullName) // Returns ‘HELLO WORLD’ vs results from getMember()

});```

Unfortunately that didn’t work as it returns UNDEFINED

var UserEmail 
var FullName

async function GetEmail (){
    await currentMember.getMember()
        .then((member) => {
            const id = member._id;
            let UserEmail = member.loginEmail;
            let FullName = `${member.contactDetails.firstName} ${member.contactDetails.lastName}`;
            console.log(id) //WORKS
            console.log(FullName) //WORKS
            console.log(UserEmail) //WORKS
            return member;
        })
    .catch((error) => {
        console.error(error);
    });
}

$w.onReady(function () {
    GetEmail();
    console.log(UserEmail) //RETURNS UNDEFINED
    console.log(FullName) //RETURNS UNDEFINED
});

Even when I put it under OnReady, it doesn’t store after the .THEN statement


$w.onReady(function () {

    currentMember.getMember()
        .then((member) => {
            const id = member._id;
            let UserEmail = member.loginEmail;
            let FullName = `${member.contactDetails.firstName} ${member.contactDetails.lastName}`;
            console.log(id) //WORKS
            console.log(FullName) //WORKS
            console.log(UserEmail) //WORKS
            return member;
        })
    .catch((error) => {
        console.error(error);
    });

    console.log(UserEmail) //UNDEFINED
    console.log(FullName) //UNDEFINED
});

I was able to make the code work, sharing for others

var UserEmail 

$w.onReady(async function () {

UserEmail = await currentMember.getMember()
        .then((member) => {
            let UserEmail = member.loginEmail;
            return UserEmail;
        })
    .catch((error) => {
        console.error(error);
    });
});

I could now use UserEmail (or any other user attribute) outside the .THEN call.