Return from function

Hello,

I am trying to retrieve the active user’s email address, which is then used to find the user’s unique id created for the site (different from the id assigned by Wix).

While this code work in terms of finding the right Id, what it returns is described as “Undefined” when I call the function from onReady.

This is the search code/function:
export function getUserInfo2 ( activeUserId ) {
wixData . query ( “Members/PrivateMembersData” )
. eq ( “_id” , activeUserId )
. limit ( 1 )
. find ()
. then ( ( results ) => {
activeUserEmail = results . items [ 0 ]. loginEmail ;
wixData . query ( “dbUsers” )
. eq ( “emailAddress” , activeUserEmail )
. limit ( 1 )
. find ()
. then ( results => {
if ( activeUserEmail === results . items [ 0 ]. emailAddress ) {
userUpId = results . items [ 0 ]. upliftId ;
return ( userUpId );
//$w(“#txtTestLogin”).text = "Hello " + results.items[0].userName;
}
})
});
}

And this is how I am calling the function in onReady:

( async () => {
console . log ( "User Id within the site: " + await getUserInfo2 ( activeUser . id ))
})();

What am I doing wrong?

Thank you in advance.

Amine

You should not nest promises but chain them.

WRONG:

export function x(arg){
    query1.find().then(res => {
        query2.find()
        .then(res => {
            return something;
        })
    })
}

RIGHT:

export function x(arg){
    return query1.find().then(res => {
        return query2.find();
    })
    .then(res => {
        return something;
    })
 }

Thank you J.D.! That resolved the issue, and I learned something new.
All the best.