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

113
Views
Get documents name in Firestore and create nested object

This question is related to this one: Get documents names in Firestore but I can't comment yet.

I am trying to get a nested object from my FireStore which looks like this

{"docname1": {…}, "docname2": {…}, "docname3": {…}}

I have this now but cant seem to figure out how to create the nested object to push it into the empty array.

 useEffect(() => {
    const getPlants = async () => {
        let response = []
        const querySnapshot = await firebase
            .firestore()
            .collection('plants')
            .get()
        querySnapshot.forEach((doc) => {
            let newObject = [doc.id: doc.data() ]
            response.push(newObject)
        })

        setPlants(response)
    }
    getPlants()
}, [])

Any help is greatly appreciated :)

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

0

I think you are creating the new object (newObject) wrong. Try to create it like this:

   querySnapshot.forEach((doc) => {
       const newObject = {}
       newObject[doc.id] = doc.data()
       response.push(newObject)
   })

Or, you can do this:

   querySnapshot.forEach((doc) => {
       response.push({ [`${doc.id}`]: doc.data() })
   })
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!