[ISSUE] wixUsers.currentUser.id undefined?

Sometimes when I execute the below piece of code, wixUsers.currentUser.id equals undefined.

The code can’t be more basic, so this again seems to be an inconsistency/glitch on the side of the rendering engine.

var visitorId;
var memberId;
var role;
var user
var isLoggedIn;

$w.onReady(async function () {

	user = wixUsers.currentUser;
	isLoggedIn = user.loggedIn;
	if(!isLoggedIn){
		visitorId = user.id;
		role = user.role; //Member, Visitor or Admin
	} else {
		memberId = user.id;
		role = user.role; //Member, Visitor or Admin
	}
	console.log("user:")
	console.log(user);
});

Hi, I hope you’re getting undefined when user is not logged in. This is obvious that if a user is not logged in then you will be unable to get the user id.

@wix-expert-velo-cert it’s not obvious, it should be a visitorId.

Could you please send the screenshots of both logged in & without login results? Also try to use console.log() at different place to get the point of error.

@wix-expert-velo-cert

To answer your questions:

  1. I’m not logged in, so logged in case is irrelevant (I’m certain of that because otherwise I’d generate a memberId and role would equal ‘Member’

  2. I am using console logs, see screenshot

This really is a glitch when I refresh my page - in almost 99% of the cases, there’s a long string generated instead of undefined. Hence in most of the cases it works fine.

I talk about a glitch because I cannot reproduce it.

Anyone?

I’ve tried this repeatedly (many many times) and had no luck in reproducing the problem. Is there any pattern - even a weak one? browser/VPN/Network? Anything?

I’d like to refer this to QA for their opinion and evaluation. Please post the URL.

If you really need the id, as a workaround you could check if the id is null, and if so, refresh the page with wixLocation.to() to the same page.

@yisrael-wix I cannot make a MWE for this and the logic is again too complicated. Can we have a call to walk through the issue, or how do you wish to proceed? As you know I have quite some other open issues that all remain unresolved as well. We’re not making any headways this way. Better I give a demo.

@yisrael-wix not an option.

@yisrael-wix I see the same issue on this page: [https://www.pinenlime.com/home-2](https://www.pinenlime.com/home-2

Please)

What’s odd is that it works on some of our other pages sporadically.

Please check the logs in the browser console itself. This is the code that’s running on masterPage.js

The same issue also happens on our home page code, and it isn’t limited to masterPage.js

Surprisingly the issue doesn’t happen on https://www.pinenlime.com/session-test




$w.onReady(async function () {
    // Write your code here
    let current_user = await wixUsers.currentUser;
    console.log("Used Await")
    let userId_not_logged = current_user.id;
    let isLoggedIn = current_user.loggedIn;
    console.log("master page Current User:" + userId_not_logged)
    var inError = false;
    //let userId_not_logged = null;
    //404-error

    if (!userId_not_logged && ("https://www.pinenlime.com/internal-error" !== wixLocation.url)) {
        if ("https://www.pinenlime.com/home-2" === wixLocation.url) {
            console.log("On Home 2")
        } else {
            console.log("master page", userId_not_logged)
            wixLocation.to("/internal-error");
            inError = true;
        }

    }

I see the same issue on our home page as well: https://www.pinenlime.com/

We’ve had to use an ugly hack in order to generate a user ID by redirecting them to a page that sometimes is able to generate it.

I’m not sure of what else to change in order to make this work. It seems like an issue on Wix’s side.

What do you mean by “the same issue”? What is working? What is not? Is this for a logged in user or for an anonymous visitor? Are you testing in Preview or Live?

BTW - it should not affect the execution of the code (although theoretically it might), you should not have an await on the following line of code:

let current_user = await wixUsers.currentUser;

You want this instead:

let current_user = wixUsers.currentUser;

Using an await is wrong - wixUsers.currentUser does not return a Promise.

@yisrael-wix I was referring to this issue: wixUsers.currentUser.id equals undefined.
I’m testing on Live and this is for an anonymous visitor.

I’ve tried it both with and without await . Both have the same issue where currentUser.Id returns undefined

I would not expect an anonymous user to have an id.

Bugs and system issues should be reported to Wix Customer Care . They should be able to assist you with this.

We’re using current user as defined here : https://www.wix.com/velo/reference/wix-users/introduction

user.id is supposed to return a unique session ID for site visitors, even if they aren’t members.

There are three types of users:
Visitor - A user who is not logged into your site.
Member - A user who is logged into your site.
Admin - The owner of the site.

We use the visitor ID to allow users to make purchases on the website even without logging in.

I’ve raised it with Wix Customer Care as well, but it’s been a while and they haven’t come back with a solution and it’s really affecting our website experience.
I thought maybe there’s another solution to it that we could use

Thanks for looking into it!

As you can see in a comment I made earlier, I was unable to reproduce this issue. However…

What you can do is to try a timeout delay. Maybe the currentUser isn’t quite initialized when the onReady() function is triggered. Something like this:

let current_user;
setTimeout(() => {
   current_user = wixUsers.currentUser;
}, 100);

I put 100 milliseconds, but you might want to play around with number. I would say that if it works with a delay, then there is an issue with currentUser.

If you end up getting this to work with the delay, let me know and I’ll escalate this issue to have it evaluated.

@yisrael-wix I tried running this at an interval, but it still didn’t return a value on the home page. I tried it at multiple intervals between a 100 milliseconds and 10 seconds.
It just came up as undefined multiple times.
I’ve also tried getting the current User ID from the backend using wix-users-backend , but that comes up as undefined too.

Can you try reproducing the error on https://www.pinenlime.com/home-2 ?

The issue here is 100% reproducible for me. It’s a copy of the home page [https://www.pinenlime.com/ ] without any components or custom code.

The logs are based on the code snippet that I shared earlier from masterPage.js
The issue’s been happening on the website for almost a month now. It happened back in December too, and the Wix Support team was able to help us resolve it, so I really doubt it’s because of a code change on our side. I’m not sure what’s causing it. It seems like a platform issue.

@pinenlime At least in the browser console I see null. It’s either a bug, or it’s the expected behavior - not logged in, no userId. You really should check with Wix Customer Care . so they can evaluate.

@yisrael-wix It isn’t expected I think, because it shows up correctly on this page in the same case. Not logged in, no user ID: https://www.pinenlime.com/session-test

I think Wix Customer Care is the way to go for this one. Will reach out to them. Thanks again!

@pinenlime The home-2 page calls a backend file that gets the current user. The backend will probably return null since the server is not aware of an anonymous user. However, it seems that to the browser (frontend), an anonymous user gets an id.

Try getting the current user on the home-2 by getting the current user in the frontend, just like session-test.

If you need further clarification, contact Customer Care.