I'm trying to create a collection in firestore, but the field section inside firestore comes out empty. The collection i.e 'user' and doc i.e 'email' section is created, but with 'field' section corresponding to the doc 'email' empty(it must show watchList:[]. I want the setDoc function to store the watchlist(corresponding to different emails). I want this to happen when the user signsIn. Please help. Thank you
e.preventDefault();
try {
const result = await createUserWithEmailAndPassword(
auth,
email,
password
);
navigate("/account");
console.log(result);
dispatch(
storeUser({
email: result.user.email,
})
);
return await setDoc(doc(db, "users", email), {
watchList: [],
});
} catch (error) {
alert(error.message);
}```
My guess is that the dispatch is also running asynchronously. If that is the case, you can use the result.user.email value in the database call too:
return await setDoc(doc(db, "users", result.user.email), {
watchList: [],
});