'adding 2 documents to firestore one is using a reference from the other?
I am trying to created 2 docs at ones what first docs id is used in the second. One problems is my code look wired, especially the chaining. I am afraid even if it works that it might be wrong.
Any idea for a more clear refactor? The flow is the following:
- signup a user
- get his created id created a new collection
- get the new collection id then
- create the final doc with the user id and with id of the previously created collection as a reference in the last doc.
const Signup = async (email: string, password: string) => {
await createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const user = userCredential.user;
const colRef = collection(db, 'Shared');
addDoc(colRef, {
sharedValue: '',
}).then((res) =>
setDoc(doc(db, 'Data', user.uid), {
sharedId: res.id,
})
);
})
.catch((error) => {
console.log(error)
});
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
