#How to get document id from createDocument and use to updateDocument
85 messages · Page 1 of 1 (latest)
You can inline this line
const documentId = ID.unique();
To such
return database.createDocument(
'6426fd4b827fedc4a23',
'6426fd9b65c0b7f2c56',
ID.unique(),
userData,
);
As it's just returns string
how to get document id from creatUserDocument
Next change the function code to await so the document will return and the promise as such
const creatUserDocument = async userData => {
try {
return await database.createDocument(
'6426fd4b827fedc4a23',
'6426fd9b65c0b7f2c56',
ID.unique(),
userData,
);
} catch (e) {
console.error(e.message);
}
return null;
};
ok
Now every time you'll call the creatUserDocument you'll have the document id
For example
const newDoc = await creatUserDocument({});
if (newDoc !== null) {
updateUserDocument(newDoc.$id, {color: 'red', side: 2});
}
in which function should i add this
in create or update
Not in both of them
This is when you want to use those functions
getting error
'creatUserDocument' was used before it was defined
'updateUserDocument' was used before it was defined
You need to import the functions
More on that here
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
which functions should i import
Both of them
how???
This code is just an example
Add it in your React code in the way it should be
i have added it
👍
This code just contains the example.
You need to add id whenever you're planning to use it
iam not getting you can you please edit my code
In a few minutes. Yes
ok waiting for your reply...
A. This is your code with the await in the functions return
import {Account, AppwriteException, Client, Databases, ID} from "appwrite"
const client = new Client()
client.setEndpoint('https://api.printfc.in/v1') // Your API Endpoint
.setProject('64242c801cdd58d2621');
const storage = Client.storage;
const database = new Databases(client);
const getUserData = async () => {
try {
const account = new Account(client)
return account.get()
} catch (error) {
const appwriteError = AppwriteException;
throw new Error(appwriteError.message)
}
}
const login = async (email, password) => {
try {
const account = new Account(client)
return account.createEmailSession(email, password)
} catch (error) {
const appwriteError = AppwriteException;
throw new Error(appwriteError.message)
}
}
const register = async (email, password) => {
try {
const account = new Account(client)
return account.create(ID.unique(), email, password)
} catch (error) {
const appwriteError = AppwriteException;
throw new Error(appwriteError.message)
}
}
const creatUserDocument = async userData => {
try {
return await database.createDocument(
'6426fd4b827fedc4a23',
'6426fd9b65c0b7f2c56',
ID.unique(),
userData,
);
} catch (e) {
console.error(e.message);
}
return null;
};
const updateUserDocument = async (documentId, {color, side}) => {
try {
return await database.updateDocument(
'6426fd4b827fedc4a23',
'6426fd9b65c0b7f2c56',
documentId,
{color, side},
);
} catch (e) {
console.error(e.message);
}
return null;
};
export {
getUserData,
Client,
storage,
register,
login,
creatUserDocument,
updateUserDocument
};
B. Anywhere else - not in that file - you can use those functions like that
This is just an example you need to adapt it to your needs and code
const newDoc = await creatUserDocument({});
if (newDoc !== null) {
updateUserDocument(newDoc.$id, {color: 'red', side: 2});
}
You just added await
Yes
And 2 null returns
My issue is I want to add documentid
Sure
Use this way to solve it
Where should I use this code
Where in your code you call the creatUserDocument function and the updateUserDocument one?
I will use them in different components
Then let's separate it to two things
-
The Appwrite part in which you can use as I showed you.
-
The React part in which you want to able to share the document id between components. For that you will need to refer to React guide about that
https://react.dev/learn/passing-data-deeply-with-context
Is that right?
this is my appwrite part
this is my signup component in which username and email will be stored in database .Here iam using createDocument
this is my select component in which user give input through radio buttons .Here i will use updateDocument to store it in appwrite user previous documentid.
now i need to get documentid from signup component to select component to update my document
this is my problem
I see, The second thing is indeed something related to react
I didn't developed with React in a long time. I won't be able to help with that topic in the code level.
What I do know is that you need to create a global state and update and set the document ID within it
Everything is working fine when I manually enter the documentid
It was storing properly without any errors
Yes, of course.
The Appwrite part is okay, the only thing you need to add is the ability to save and retrieve the ID from the React side.
Then it will be best to check with React developers, or consult the React documentations.
Can you verify my question:
- createDocument will be execute when user register?
- Then when/where the updateUserDocument be executed?
1.yes
2. updateUserDocument is executed in different component
So it means that you have a user page that contains all the user info and you have this button to update the user document, am I correct?
No
- I have signup page in which username and email will be stored. Here I have used createDocument
- 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
If you use next.js then I think the best way is to put the documentId in the url parameters or query string. Otherwise you can use the react router tanstack. This is the efficient way to store the documentId
Can you please help me how to do that
Oh I see I understand now
Help me how to do that in react router
actually you can use the function feature of appwrite.
- You have to create a function named 'Create User Profile' or it depends on your preference. And set this function to listen to the event from Appwrite Authentication.
- Now, since the
createaccount method will always return an User Object, which this object contains $id attribute, then instead of usingID.uniqueincreateUserDocument, you can use the $id from User Object - Therefore in your react, you can use the
account.getmethod since this method also return aUser Objectwhich contain$idas well.
Iam unable to get you
I will do my best to provide you a code, but this takes me a while
Ok I will wait for you
Iam struggling with this issue from past 2 weeks
This is my appwrite component
This is my signup component in which username and email will be stored
This is my select.js another component in which I want to store user input through radio button . Here iam using updatedocument
This is the simulation example of create account. and every there is new user in your backend, our function will be triggered
This is the code how I did implement the funciton
https://gist.github.com/moshOntong-IT/6ba791ddd1b3f1b4b758e44e1148152b
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
Iam new to appwrite I don't know about it
Did you understand my issue
- I have signup page in which username and email will be stored. Here I have used createDocument
- 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
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
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,
}
);
I have no idea about cloud functions
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
Yes I have seen
If that case okay then I will modify your code.
This is my githib repo
In this I want to store user input from different components
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