Get username from module to wix page

Hello.every one . I have build a module to work with userdata i need to return only with username in case he login else return with error or any other message
this is my module

import wixUsers from ‘wix-users-backend’ ;
import wixData from ‘wix-data’ ;

var username ;

export async function CheckUsername () {

let user = wixUsers . currentUser ;
let userId = user . id ;
let isLoggedIn = user . loggedIn ;
let userdata = wixUsers . getUser ( userId );
let steamIDJSON = userdata
. then (( steamIDJSON ) => {
username = ( steamIDJSON . memberName );
// console.log(username);
});
return username ;

}

console with log print user name and works perfect while return back with returned with undefined
despite username is gloable variable
where is the error comes from ?

I have solved it only call the field you want without any loop

Hi @alaaalrma ,

In functions, you need to return the value to get it out of the function, otherwise, it’ll remain inside the function’s code block and return void.

const getName = () => {
    return 'Ahmad Nasriya';
}

const getAge = () => {
    25
}

Now when assigning a constant the value of calling the function, then …

const my_name = getName(); // my_name = 'Ahmad Nasriya'
const my_age = getAge(); // my_age = undefined;

The reason why the second constant value was undefined is because the getAge() function doesn’t return anything.

So you always need to return a value from the function if the main purpose of the function is returning a value.