User ID undefined ?

Hello,

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…

Why ? Am I missing something ?

Thanks for your help !

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 :sweat_smile:. Thanks

Just a tought but maybe wix session can be the solution to this.
It can save data while on the page.

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 :thinking:. I’m not so good at coding :sweat_smile:

Just a few days ago, i had a similar issue, perhaps this post can help you out…

Almost sure, that you want to do the same, or a similar function, like shown in the link.

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.

@russian-dima Great ! Yes it seems to be what I’m looking for. I’ll try that as soon as possible and let you know. Thanks

@volkaertskristof Good idea ! I’ll investigate it and let you know . Thanks !

@russian-dima @volkaertskristof

So I tried with the session and arrays and it works “ok ish”.

Here is my code below.
The principle is that when I click a “add to cart” button, the object is stored in the array and the array in the session.

When I add another product to the cart, I get the array from the session, push the new object and store the new array back to the session.

It works but it certainly not the cleanest code :sweat_smile:

$w.onReady(function () {

let itemObj = $w("#dynamicDataset").getCurrentItem();

let array=[];
let arraySession = session.getItem("Array");

$w("#addDB").onClick( (event) => {

if(arraySession === null){
array.push(itemObj);
session.setItem("Array",  JSON.stringify(array));
}else{
    arraySession = session.getItem("Array");
 let arrayItems = JSON.parse(arraySession);
    arrayItems.push(itemObj);
    session.setItem("Array",  JSON.stringify(arrayItems));
}
} );

});

When I go to the “cart page”, I retrieve the array and show the data in a repeater.

:thinking:
MY LAST PROBLEM is that I try to remove a specific item from the cart when I push a button. I use the splice () function.

$w.onReady(function () {
 let arraySession = session.getItem("Array");
 const arrayItems = JSON.parse(arraySession);

function populateRepeater() {

      $w(`#repeater1`).data = arrayItems;
      $w(`#repeater1`).onItemReady(($w, itemData,index) => {
       
        $w(`#text8`).text = itemData.title;
        $w("#image1").src = itemData.image;
        $w("#removeBtn").onClick((event) => { 
            arrayItems.splice(index,1);
            console.log(arrayItems);
            populateRepeater()
        });
      });
 
}
populateRepeater()

My code above is not working well. The item is not removed from the session but only from the repeater…

session.removeItem("key");

How can I remove a specific item from the array with a key “Array” from the session ?

Thanks :grinning:

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 :wink:

Your question is totaly confusing!

“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).

$w.onReady(function () {
 let arraySession = session.getItem("Array");
 const arrayItems = JSON.parse(arraySession);

function populateRepeater() {

      $w(`#repeater1`).data = arrayItems;
      $w(`#repeater1`).onItemReady(($w, itemData,index) => {
       
        $w(`#text8`).text = itemData.title;
        $w("#image1").src = itemData.image;
        $w("#removeBtn").onClick((event) => { 
            arrayItems.splice(index,1);
            console.log(arrayItems);
            populateRepeater()
        });
      });
}
populateRepeater()

That means, that you have already a new array/object-value with one values less!

All you have to do now is to write it back to session. Is this what you wanted to do ?

And last question is → "Why do you have 2x populateRepeater() ???

@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 ^^).

Here is my code if it can help someone else.

function populateRepeater() {

 let arraySession = session.getItem("Array");
 const arrayItems = JSON.parse(arraySession);
  console.log("arraySession",arrayItems)


      $w(`#repeater1`).data = arrayItems;
      $w(`#repeater1`).onItemReady(($w, itemData,index) => {
        $w(`#text8`).text = itemData.title;
        $w("#image1").src = itemData.image;

 const deleteButton = $w("#removeBtn");
        deleteButton.onClick((event) => { 
            arrayItems.splice(index,1);
            session.setItem("Array",  JSON.stringify(arrayItems));
            populateRepeater()
        });
      });
 
}

And you’re right with the 2 populateRepeater(). It was for another button that I don’t use anymore. My bad.

Thanks for your help !

@costino80
Well done! And surely there will be someone who will need this code, because of beeing in the same situation.