Thomas
Thomas

Reputation: 35

how to add another collection data with in a doc id in firebase using express node js

I am trying to add another collection of data with in a doc id in firebase below is the database path for referance.

screams/(doc_id)/(users)

please find the below api post call to add the data.

 app.post('/screams/:id',(req,res)=>{
   const mark ={
       Mark:req.body.Mark,
       Name:req.body.Name
   }
    db.collection('screams').doc(req.params.id).doc('users').add(mark)
    .then(doc=>{
        return res.status(200).json({handle: 'data added successfully'})
    })
})

when i tried to add the data its giving me an error

TypeError: db.collection(...).doc(...).add is not a function
    at app.post (/srv/index.js:141:49)

please help me to sort out the issue. if you can also suggest me a way to get the added data would be great :)

Upvotes: 0

Views: 271

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83153

add() is a method of a CollectionReference, but with

db.collection('screams').doc(req.params.id).doc('users')

you are not defining a CollectionReference, since you call twice the doc() method.


You need to call the collection() method of the DocumentReference defined by db.collection('screams').doc(req.params.id), as follows:

app.post('/screams/:id',(req,res) => {
   const mark ={
       Mark:req.body.Mark,
       Name:req.body.Name
   }
    db.collection('screams').doc(req.params.id).collection('users').add(mark)
    .then(docRef => {
        return res.status(200).json({handle: 'data added successfully'})
    })
})

Upvotes: 1

Related Questions