'wixUsers.currentUser' returns undefined

Hi,
I am relatively a newbie and I am facing an issue retrieving currentUser data from wix-users api.
I searched all over the community and docs for a possible solution but couldn’t find one. Request someone’s help here!!

‘wixUsers.currentUser’ returns undefined in one of my page but the same code works fine in my home page.

Strangely it works fine on both the pages in PREVIEW mode but not in live site :frowning:

Works here https://dineshbabuthangave.wixsite.com/website
but not in https://dineshbabuthangave.wixsite.com/website/package

import wixData from 'wix-data';
import wixUsers from 'wix-users';
$w.onReady(function () {
	let user = wixUsers.currentUser;
	let userId = user.id;
	debugger;
	loadDataSelectionTag();
});

Thank you.

This is from the wix-users API :
The APIs in wix-users are only partially functional when previewing your site. View a published version of your site to see their complete functionality.

The reason that it works on both pages in Preview is that when you are in the Editor you are the current user and the information is available. If currentUser is undefined on a page, that would mean that the a user is not currently logged in.

Hi Yisrael,
Thank you for such a quick answer.
Here in live site I am logged in and my home page is able to recognize it but not the other page.

If possible you can shortly take a look on the code in my editor.

Thank you

I am of course unable to run your site in Live mode…

What isn’t working on the package page? How do you know that the current user is undefined?

Did you try a console.log statement to see what current user id is:

import wixData from 'wix-data';
import wixUsers from 'wix-users';
$w.onReady(function () {
	let user = wixUsers.currentUser;
	let userId = user.id;
	console.log('userId', userId);
	loadDataSelectionTag();
});

BTW - For proper site security, I would also recommend using backend code for getting the current user and using the userId for a database query. See w ix-users-backend API for more information.

For information on using backend functions, see Corvid Web Modules: Calling Server-Side Code from the Front-End .