I am using google firebase and node.
I am writing a function that returns a particular piece of data depending on the id given in the request.
I have a collection on google firebase called screams and I also have a collection called comments. One of the fields in my comments collection is called screamId. if the id in this field matches the id of the scream id I use in my request it needs to push the data of that document into an empty array I have set.
on Postman, it is successfully giving me back my piece of data I requested(the individual scream document) but it is not pushing the comment document from the comments collection into the empty array I set. Instead, I am just getting the empty array back
Here is what I get back from Postman:
{
"body": "ice scream",
"createdAt": "2020-01-27T17:56:11.885Z",
"userHandle": "joel",
"screamId": "cdQnSrHtsRzYmKL6dSoS",
"comments": []
}
there should be an object inside that empty array containing my comment document from my database.
here is the code to my function:
exports.getScream = (req, res) => {
let screamData = {};
db.doc(`/screams/${req.params.screamId}`)
.get()
.then(doc => {
if (!doc.exists) {
return res.status(404).json({ error: "Scream not found" });
}
screamData = doc.data();
screamData.screamId = doc.id;
return db
.collection("comments")
.where("screamId", "==", req.params.screamId)
.get();
})
.then(data => {
screamData.comments = [];
data.forEach(doc => {
screamData.comments.push(doc.data());
});
return res.json(screamData);
})
.catch(err => {
console.error(err);
res.status(500).json({ error: err.code });
});
};