hidjou / classsed-react-firebase-functions

327 stars 180 forks source link

comments collection not querying with .where(), returning blank #2

Open trunderman opened 5 years ago

trunderman commented 5 years ago

When I remove the .where() from the below it get all the comments returned, but when I restore it and have the where() query, I get a blank comments response. Followed the tut exactly.

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')
        .orderBy('createdAt', 'desc')
        .where('screamId', '==', req.params.screamId) //if i remove this, the functionality works
        .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 });
    });
trunderman commented 5 years ago

@hidjou, ive been stuck on this for 2 days now, and have reinspected my code 100x over, any help would be appreciated. Tutorial is very helpful and appreciated

Darshilp326 commented 4 years ago

@trunderman how did u solve this bug?

thilanka commented 4 years ago

Only reason is you don't have matching data in database for that parameter. check your database.

MrNovember22 commented 3 years ago

@trunderman @Darshilp326 Just comment this line .orderBy('createdAt', 'desc') Right now I don't know why the sorting is not working. But If you remove it, then comment will be shown. I suppose there is an issue with createdAt field, but I tried to set it not via String, and use the Timestamp, but it's still not working properly.

@thilanka I've checked my database, and everything is matching and configured properly.