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

261
Views
How to create a collection in FireBase using React

The problem is pretty simple. I want to create a document that has a collection in it. An alternative way of wording it would be I need to make a collection in a document.

I need to be able to do this in React.

Visual: collection -> document -> collection

Here is where I need to add the code:

const addUser = async () => {
    await addDoc(usersCollectionRef, {
      name: newName,
      grade: Number(newGrade),
      role: newRole,
      hours: Number(newHours),

      //Collection Creation should go here as this is where I am making the new document.

    })
  }

Note: This is react and not react native

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

0

If I understood you correctly, you want to make a new collection inside the doc you just created in your code above. A collection cannot be empty, therefore you have to put some data inside a document in your collection. One way this can be done is like this:

const usersCollectionRef = colleciton(db, 'users')

const addUser = async () => {
    const document = await addDoc(usersCollectionRef, {
      name: newName,
      grade: Number(newGrade),
      role: newRole,
      hours: Number(newHours),
    })

    const newCollectionRef = collection(db, 'users', document.id, 'name of new subcollection')

    await addDoc(newCollectionRef, {
        data: 'Hello World!',
    })
  }
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!