I’m trying to get a user ID from regular visitors (not logged in) by using this code :
import wixUsers from 'wix-users';
$w.onReady(function () {
let user = wixUsers.currentUser;
let userId = user.id;
console.log("user ID", userId);
});
In preview mode, it works just fine and I have an ID, but on the published website it’s totally random. Sometimes I have an ID, but most of the time the ID is undefined…
In Preview, you will get your own (logged in) _id. In Publish, there is no _id for non-logged in users. Sometimes it’s empty, sometimes a crazy long string, but both are unusable.
It comes down to this: a non-logged in user (“Visitor”) does not have a _id.
Thanks for the reply. Is there a way to have any kind of specific information about the visitor (session id, …? ). My visitors can add data in a database collection and I‘d like them to be able to retrieve what they submitted (like a shopping cart). Wix Store can do that… (so Wix has a way of knowing who is visiting (even not logged in). I hope it makes sense . Thanks
I already thought about that but there is not so much explained about Wix Session in the Velo documentation. I’d like to store multiple items and retrieve them in a repeater. So maybe use an array of objects . I’m not so good at coding
Maybe instead of using an id (wich wont work for visitors try to ise their ip adress. This way you have a unique key for the visitor’s device.
Heres a tutorial i found.
I never used the session part myself so i don’t really know how to do this.
But if i have the time i will try it out myself and make a small tutorial about it.
This way i learn a bit to
“How can I remove a specific item from the array with a key “Array” from the session ?”
You have already “successfully” removed the item from the array/object (within your repeater).
@russian-dima Thanks for your answer. Thats what I did finally yes ! I thought it would be maybe easier to remove an item from the session with simple code (dreaming ^^).