Create a user id

Good morning!

I would like to create a id for my users when they register on the site. Something like “Abc_001”, “Abc_002”.

I tried to use hooks however, the “after_insert” did not work through the form in the site and the “after_query” applied the same id to all rows.

Thanks!

First of all, you should be aware that the wix-user API automatically assigns a unique user ID to each registered user.

Concerning your efforts, why are you using afterInsert ? I would think you would want beforeInsert in order to save the newly assigned ID to the database.

Post your code so we can evaluate.

Hi Yisrael!

I’m starting in Wix Code and I really did not know of wix-user api.

My idea is to create a custom id because the id pattern is not very friendly (“r5cme-6fem-485j-djre-4844c49”), the id should represent my associates, each one would have its id on an id card.

The code I tried is very simple, it was only for testing:

export function Associates_afterInsert (item, context) {
item.rgnr_id = ‘rgnr-0’ + 1
return item
}

But i want to do auto increment.

Thank you for listening

Due to the possiblity of simultaneous access of multiple users, there is a small chance of duplicate entries so this is really not recommended. You can refer to the forum post Numbered index for more details and an explanation on how to create auto-incremented identifiers.

I hope this helps,

Yisrael

1 Like

Thanks!

No problem. BTW- I believe that the developers are working an a way to protect against simultaneous access, however this is not a simple matter and there is no timeline on when this will be available.

1 Like

I will try this approach in other moment, the link about auto_increment gave me new ideas for another collections.

As far as a readable and unique user ID is concerned, you could use the user’s email as a user ID. This is common with many sites. It’s not just readable, but it’s also more readable than “Abc_001” as well as easier to remember.

I will suggest this idea. I think it is a good way.