Back

const id = ID.unique(); does not working ?

  • 1
  • Web
yalondpsi
23 Sep, 2023, 18:19

I need to store a unique id in a const. All I get back from const id = ID.unique(); is "unique()" What am I doing wrong? πŸ™‚ Thank you

TL;DR
User is trying to generate a unique ID using `ID.unique()`, but it is not working as expected. They are making several requests to create usernames, user data, and a new user account, and they want all those collections to have the same unique ID. They are unsure how to store the unique ID in a constant. The solution is to use `account.create(ID.unique(), 'email@example.com', 'password')` to automatically generate a unique ID for the user and store it in a constant.
Drake
23 Sep, 2023, 21:42

Nothing, this is expected

yalondpsi
24 Sep, 2023, 05:33

So, in that case should I use uuid npm package or a regular expression?

safwan
24 Sep, 2023, 05:36

You don't need to use uuid. ID.unique() generates a unique ID for you

safwan
24 Sep, 2023, 05:36

I need to store a unique id in a const What exactly do you need this unique ID for?

yalondpsi
24 Sep, 2023, 05:41

@safwan Hi, The main goal is to create user names when registering a new user. I am making several requests one after another to create a usernames record and then user's data and in the end, creating a new user account. All those collections share the same user's unique ID

safwan
24 Sep, 2023, 05:45

Well then you can just do:

TypeScript
const promise = await account.create(ID.unique(), 'email@example.com', 'password');

This is will automatically generate the unique ID for the user, and account.create will return a User Object, which includes the generated ID in the $id attribute

yalondpsi
24 Sep, 2023, 05:47

Yes of course, I know that, but because this is my last request in the chain, I don't have the id. This is the last request because I want to be able to cancel the other request if something went wrong

safwan
24 Sep, 2023, 05:48

Once again, you can get the ID from the user object that is returned.

safwan
24 Sep, 2023, 05:50

I'm assuming you just want the ID. You don't need to assign ID.unique() to a variable.

TypeScript
const promise = await account.create(ID.unique(), 'email@example.com', 'password');
const userId = promise.$id;
yalondpsi
24 Sep, 2023, 05:54

Let me ask you something important πŸ™‚ I am making the account.create ... call last because I read the documentations and did not find a way to delete a user from the client side (only server side) if something went wrong. I can make your example the first call and get the ID. But what if the 2 calls after that are got an error? How do I "undo " account.create ... ?

safwan
24 Sep, 2023, 05:55

But what if the 2 calls after that are got an error? Are these two calls user-specific actions like creating a document, adding default user preferences or something like that?

safwan
24 Sep, 2023, 05:56

Regardless of that, the reason deleting a user is available only on the server-side is for obvious security reasons.

safwan
24 Sep, 2023, 06:00

So if you want to delete a user at any point in time, you can do this:

  • Create a Cloud Function that uses the Users API. Specifically the Delete User endpoint. Make sure it grabs the userId from the request payload.
  • You can execute this function from the client-side using the Create Execution endpoint of the Functions API. Make sure to send the userId in the request payload
safwan
24 Sep, 2023, 06:01

In your particular use-case, wrap all of the calls you are making after the account.create in a try-catch and have the functions.createExecution in the catch.

yalondpsi
24 Sep, 2023, 06:05

First, thank you very much for the help. I will try that @safwan

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more