I’m using "where" query to get a single document so that I can update it. I need the docID which is what i’m struggling with. I’m following the documentation but not much progress. I’m using Firebase cloud functions, Nodejs and Javascript.
const admin = require("firebase-admin");
const firestore = admin.firestore();
exports.bucketTesting = functions.https.onRequest(async (req, res) => {
//code to source file.metadata.mediaLink
const imagesRef = firestore.collection("images");
const specificImageRef = await imagesRef
.where("imageUrl", "==", file.metadata.mediaLink)
.get();
console.log(specificImageRef);
specificImageRef.forEach((doc) => {
console.log(doc.id, "=>", doc.data());
});
}
The console.log(specificImageRef)
print data(that i can’t use) but the console.logs
in the forEach do not print anything.
How do I get the docID?
2
Answers
Your code is correct to get the document docID and data but as discussed in the comments the query returns 0 documents.
The only adaptation you could do since you are sure that there is only one document corresponding to the query is to use the
docs
array as follows: