Back

How to get document id from createDocument and use to updateDocument

  • 0
  • Databases
  • Web
bharath
14 Apr, 2023, 16:04

Ok I will wait for you

TL;DR
The user is asking for help on how to retrieve the document ID after using the createDocument function in Appwrite, and then use that ID to update a document in another component. Another user suggests using a cloud function to optimize the code and avoid issues with storing the document ID in the browser. The second component mentioned is Select.js, where the user wants to store user input using radio buttons and update the document with the retrieved document ID. The user also provides links to their GitHub repo and code examples for reference.
bharath
14 Apr, 2023, 16:04

Iam struggling with this issue from past 2 weeks

bharath
14 Apr, 2023, 16:04

This is my appwrite component

bharath
14 Apr, 2023, 16:05

This is my signup component in which username and email will be stored

bharath
14 Apr, 2023, 16:06

This is my select.js another component in which I want to store user input through radio button . Here iam using updatedocument

Mosh Ontong
14 Apr, 2023, 16:20

This is the simulation example of create account. and every there is new user in your backend, our function will be triggered

Mosh Ontong
14 Apr, 2023, 16:20
Mosh Ontong
14 Apr, 2023, 16:21

This is the code how I did implement the funciton https://gist.github.com/moshOntong-IT/6ba791ddd1b3f1b4b758e44e1148152b

Mosh Ontong
14 Apr, 2023, 16:22

And if you are not familiar with appwrite function, there is a resources on internet on how to use the appwrite function, and if you are ready to go like you are already familiar with appwrite function, just mention me there and I will help you

bharath
14 Apr, 2023, 16:29

Iam new to appwrite I don't know about it

bharath
14 Apr, 2023, 16:29

Did you understand my issue

bharath
14 Apr, 2023, 16:39
  1. I have signup page in which username and email will be stored. Here I have used createDocument
bharath
14 Apr, 2023, 16:39
  1. In another component (Select.js) in this component user input through radio buttons . Here I will updateDocumnet but I need documentid to do that as this is another component I need documentid
Mosh Ontong
14 Apr, 2023, 16:40

yeah, I did understand the issue here. But what I did is that to optimize your code, function will be the first step of our path way to solve your problem.

Because I believe even you store your DocumentID in your browser something like cache in web, the problem here is that what if the user refresh or exit the website? So the DocumentID stored in your cache will be deleted. Therefore I recommend you to use a cloud function to shorten your code. Because we can take advantage the account.get() method. Because if we use this method it will always return an User Object and we can use the $id attribute here, and treat this $id as your DocumentID. Because in our cloud function implementation we have this code

TypeScript
  const userObject = JSON.parse(req.variables["APPWRITE_FUNCTION_EVENT_DATA"]);
  console.log(userObject);
  try {
    const result = await databases.createDocument(
      "64397a645b2d0000f2e0",
      "64397a6ec7fce839a55c",
      userObject.$id,
      {
        name: userObject.name,
        email: userObject.email,
      }
    );
bharath
14 Apr, 2023, 16:42

I have no idea about cloud functions

Mosh Ontong
14 Apr, 2023, 16:42

Please take a look the encircle in our image attached. I did not use the ID.unique as our Document ID when creating a user account, Instead I use the $id of our User Object

bharath
14 Apr, 2023, 16:43

Yes I have seen

Mosh Ontong
14 Apr, 2023, 16:43

If that case okay then I will modify your code.

bharath
14 Apr, 2023, 16:44
bharath
14 Apr, 2023, 16:44

This is my githib repo

bharath
14 Apr, 2023, 16:45

In this I want to store user input from different components

Mosh Ontong
14 Apr, 2023, 16:45
TypeScript
const register = async (email, password) => {
  try {
    const account = new Account(client)
    const result = account.create(ID.unique(), email, password)
    console.log(result)
  } catch (error) {
    const appwriteError = AppwriteException;
    throw new Error(appwriteError.message)
  }
}

I just modify your register method , I put a result and console log here, and I temporarily remove the return here. Can you please screenshot the log in devinspector

bharath
14 Apr, 2023, 16:46
  1. Username and email (signup.js)
  2. Filename (Uploadcomp.js) 3.Color,side (Select.js)
  3. Table values (Showprice.js)
bharath
14 Apr, 2023, 16:48

This user inputs I should store

bharath
15 Apr, 2023, 02:01

Have you gone through it

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