Creating user:
const register=async()=>{
try{
const user = await createUserWithEmailAndPassword(auth, registerEmail, registerPassword)
const newUser = await addDoc(collection(db, "Users"), {
email:registerEmail
});
setEmail(newUser.email)
navigate("/home");
}catch(error) {
console.log(error.message);
}
}
I tried to store email to match it with firestore user data email
Below is other component where I try to add new collection to specific user, but fail to do so
const createAnime = async () => {
const addAnime= await addDoc(collection(db,"Users",user.id,"anime"),{
title,genre,numb,comment,rating,aid
})
}
I've tried various solutions. The goal is to add (title, genre, number, etc.) as separate documents to the user created. I fail to match the data from auth user to the same user data created in Direstore.
You are using addDoc() when adding user's document to Firestore that generates a random document ID but then trying to add a sub-collection to document with user's UID (which probably doesn't even exist). Instead set the document ID to user's UID at first place as shown below:
const register = async () => {
try {
const { user } = await createUserWithEmailAndPassword(auth, registerEmail, registerPassword)
const newUser = await setDoc(doc(db, "Users", user.uid), {
email: registerEmail
});
setEmail(registerEmail)
navigate("/home");
} catch (error) {
console.log(error.message);
}
}