I have a custom Wix profile page (made with Velo) that displays the user's information. To get that information, I have to query a database, and to query that database I have to get the currently logged in user's login email address. Fortunately, Wix has an API for that, currentMember. However, using currentMember.getMember() is returning undefined on the profile page. But after I reload the profile page, it returns an object with the correct member data (including the email I need). Why is this happening? Another thing I noticed is that I'm getting the following error in the console:
I was thinking that since the error said the URL was preloaded, perhaps the page loaded before I logged in, and thus the member object being returned is undefined, since the user hadn't logged in yet.
Here is the code I used to log the member object to the console:
import { currentMember } from 'wix-members';
$w.onReady(function () {
currentMember.getMember()
.then((member) => {
console.log(member);
});
}
And this was logging undefined, but when I reload, it gives the correct info.
I had a similar issue whereby a member logs in, but member object returned was not available until a refresh of the page occurred.
I solved this with the use of the "wix-members" onLogin() api. Below is the code I hacked together.
authentication.onLogin((memberInfo) => {
const memberId = memberInfo.id;
if (memberId) {
console.log("MEMBER ID: " + memberId);
local.setItem("auth", memberId);
} else {
currentMember.getMember()
.then((member) => {
if (member) {
console.log("MEMBER ID: " + member._id);
local.setItem("auth", member._id);
}
else {
console.log("NOT LOGGED IN!");
}
})
.catch((error) => {
console.error(error);
});
}
});
I had to use an if / else as the memberInfo.id property always appears undefined. I left it in the code (for my use case) in case it begins to work as expected again.
I used this in the masterPage.js onReady().