I am trying to use the where clause to find a document in my Firestore database from my google cloud function, this is my code below when I try to just get a document with an ID, it works but when I try to use the where clause it doesn't, how can I fix this? Am I doing anything wrong
return await firestore.collection(COLLECTION_NAME).where('APIKEY', '==', '<APIKEY_VALUE>').get().then(doc => {
if (!(doc && doc.exists)) {
return res.status(404).send({
error: 'Unable to find the document'
});
}
const data = doc.data();
console.log(data);
if (!data) {
return res.status(404).send({
error: 'Found document is empty'
});
}
return res.status(200).send(data);
}).catch(err => {
console.error(err);
return res.status(404).send({
error: 'Unable to retrieve the document',
err
});
});
Here is a screenshot of my Firestore database and the document I want to collect:
You are supposed to replace 'COLLECTION_NAME' with the name of the collection you want to find.
Replace firestore.collection(COLLECTION_NAME) with firestore.collection('accounts')