Back

ID.unique() is just producing a string of "unique()"

  • 0
  • Web
Maniac_Fighter
11 Oct, 2023, 09:59

ID.unique() indeed returns unique(). This when passed when creating a new document tells the server to generate a new ID for you. so in the response you would get the generated Id

TL;DR
User is having trouble using ID.unique() to generate a unique ID for creating a user and a document with the same ID. They are asking how to set permissions for the document and use the same ID in the code. A solution is provided by using the ID generated from creating the user. The user tried inserting ID.unique() and "unique()" directly into the createDocument() but it didn't work. The response confirms that passing ID.unique() in createDocument() will generate a new ID for the document.
Rich
11 Oct, 2023, 10:02

thanks I'll give it a try.

I did try inserting ID.unique() , and "unique()" directly into the createDocument() but neither worked.

Rich
11 Oct, 2023, 10:02

actually, that's what I already had and it didn't work

Rich
11 Oct, 2023, 10:04

i must be doing something silly - i'll try again

leonardorick
20 Oct, 2023, 15:36

I'm facing this issue now. I wanted to create a user and a document with the same id, is that possible to really generate a unique id on the format that appwrite uses to I can reuse it?

Maniac_Fighter
20 Oct, 2023, 15:37

when you create a new user, it returns a a user model response. It contains the Id. Now you can use the same id to create a new document

leonardorick
20 Oct, 2023, 16:22

Thanks @Maniac_Fighter. If I don't have the id created yet, how can I set the permissions of a document when creating it on the server? How can I use the same id on all <id> placeholders on the below code

TypeScript
return await databases.createDocument(database, collectionUsers, <id>, {
      <id>,
      email,
      name,
    }, [
      Permission.read(Role.user(<id>)),
      Permission.write(Role.user(<id>)),
    ]);
Wintermutation
21 Oct, 2023, 07:09

First you create the user with id="unique()" and the backend will replace this with the ID which you use in your subsequent createDocument call.

TypeScript
return await databases.createDocument(database, collectionUsers, ID.unique(), data={"user_id" = user["$id"]}) ```
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