0

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 });
});
};
3
  • Firebase doesn't support arrays or their methods. Check this out: firebase.googleblog.com/2014/04/… Commented Feb 3, 2020 at 20:22
  • @jmargolisvt This question is asking about Firestore (not Realtime Database), which does support array type fields in documents. The problem with this question is that we can't see the source data that's supposed the be in the array. It's also apparently using Cloud Functions, and it should say more about how everything is put together. Commented Feb 3, 2020 at 23:53
  • @DougStevenson the comments collection has four fields, all with a string as their value. I am using the exact same logic in another function and it is working fine. Firestore is recognizing forEach() and data() as Firestore methods along with recognizing the parameters to my promises as QuerySnapshot/QueryDocumentSnapshot very weird behavior... Commented Feb 4, 2020 at 0:10

1 Answer 1

1

I am having similar questions or issues. The source @jmargolisvt comes from the postman, that where I am populating the firestore from for now until later when the front end is built. See below the entire route responsible for the call. Meanwhile, firestore already has the doc I am querying.

exports.getHolla = (req, res) => {

    let hollaData = {};

    db.doc(`/holla/${req.params.hollaId}`)
    .get()
    .then((doc) => {
        if(!doc.exists){
            return res.status(404).json({ error: 'Holla not found'})
        }else{
            hollaData = doc.data();
            hollaData.hollaId = doc.id;
            return db.collection('comments')
            .orderBy('createdAt', 'desc')
            .where('hollaId', '==', req.params.hollaId).get();
        }  
    }).then(data => {
        hollaData.comments = [];
        data.forEach((doc) => {
            hollaData.comments.push(doc.data());
        });
        console.log(hollaData);
        return res.json(hollaData);
    }).catch(err =>{
        console.error(err);
        res.status(500).json({ error: err.code });
    });
};

Result:

{
    "createdAt": "2021-01-18T19:43:14.902Z",
    "body": "A new holla from Ajioz",
    "userHandle": "user",
    "hollaId": "6afT8XhfTBMdnTnHpS02",
    "comments": []
}

Expected Result:

{
    "createdAt": "2021-01-18T19:43:14.902Z",
    "body": "A new holla from Ajioz",
    "userHandle": "user",
    "hollaId": "6afT8XhfTBMdnTnHpS02",
    "comments": 
         [
           { 

            "body": "Nice Holla there"
            "createdAt": "2021-03-15T10:59:52.987Z"
            "hollaId": "6afT8XhfTBMdnTnHpS02"
            "userHandle":"user"
        
           }
        ]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.