问题
In the below code, when I remove the .where() from my query the query works and I get my comments returned. However, when I use the where query to try and find a comment bound to a post (scream) id, i get a blank array. Am I using .where() incorrectly? Any help would be appreciated.
exports.getScream = (req, res) => {
let screamData = {};
db.doc(`/screams/${req.params.screamId}`)
.get()
.then((doc) => {
screamData = doc.data();
screamData.screamId = doc.id;
return db
.collection('comments')
.where('screamId', '==', screamData.screamId) //query that doesnt seem to be working
.orderBy('createdAt', 'desc')
.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 });
});
}
Firestore document model
{
screamId: “id of post commenting on”,
body: “content of comment”,
userHandle: “username of comment”
}
来源:https://stackoverflow.com/questions/56994387/where-firestore-query-not-working-properly