Browser does not recognize the nested variable, but preview mode does.

So I can write this promise and the then() a couple of different ways and the browser won’t recognize the ‘userId’ variable as a search parameter in the .eq of the “AllowedMembers” query. I can place a string there, works just fine. It should work, especially when the promises are nested the second way. It works in the preview mode. Any thoughts why it doesnt work once published.

$w.onReady(function () {

currentMember.getMember() 
    .then((member) => { 
        userId = member.loginEmail; 
        $w("#greeting").value = userId; 
        $w("#testInput2").value = userId; 
        console.log(member) 
    }) 

    .then(() => { 
        wixData.query("AllowedMembers") 
            .eq("title", userId) 
            .find() 
            .then((results) => { 
                //if(results.items.length > 0) { 
                currentID = results.items[0].title; 
                $w('#testInput').value = currentID; 
                console.log(results) 
                //;} 
                //else { 
                // currentID="hello"; 
                // $w('#testInput').value=currentID; 
                // } 
            }) 
            .catch((error) => { 
                console.error(error); 
            }) 
    }) 

    .catch((error) => { 
        console.error(error); 
    }); 

$w("#logout").onClick((logout) => { 
    authentication.logout(); 
    console.log(logout); 
}); 
Here is pic line 22 is problem: doesnt recognize the `userId` variable once published

And here is another way of writing it that also works in preview mode but not published:
i have erased the if else functions that I tried out. line 18 problem:

Any suggestions are welcomed

so I ended up using asynchronous function with await. But the above code may have worked just fine. I would have to run it again to test it. Again, worked on preview, not on publish; as it turns out the browser queries were case sensitive, preview is not. Just an easy fix.