Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
How to add new collection to existing document React v18, Firebasev9

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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);
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!