I am trying to console.log "Not Found" if a search query in Firestore isn't found. At the moment I am using the following to perform the query which looks through all documents for an email = "[email protected]".
This works fine if the document contains what I am searching for but I cant get it to console.log anything if nothing is found
db.collection("test").where('email', '==', "[email protected]").get()
.then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
console.log(doc.data())
});
}).catch(function (error) {
console.log("Error getting document:", error);
});
CodePudding user response:
If no matching document is found then the empty
property of the QuerySnapshot will be true
:
db.collection("test").where('email', '==', "[email protected]").get().then(qSnap => {
if (qSnap.empty) {
console.log("Not found")
} else {
// proceed
}
})