Once a user registers or logs into my page, I am looking to copy the user id and email into my Associate table. But for some reason, there is no value when I call wixUsers.currentUser.id or wixUsers.currentUser.getEmail(). However, when I hard-code values (as shown commented out below) it works 100% of the time. I must be doing something wrong. This code is at the site level, not the page level. Thanks!
import wixUsers from 'wix-users';
import wixData from 'wix-data';
$w.onReady(() => {
add();
});
export function add() {
let userId = wixUsers.currentUser.id;
let userEmail = wixUsers.currentUser.getEmail();
//let userId = "test123";
//let userEmail = "test@test.com";
return wixData.query("Associate")
.eq("_id", userId)
.find()
.then((results) => {
// if an item for the user is not found
if (results.items.length === 0) {
// create an item
const toInsert = {
"_id": userId,
"email": userEmail
};
// add the item to the collection
wixData.insert("Associate", toInsert)
.catch((err) => {
console.log(err);
});
}
});
}