Firebase Cloud Functions Check Db For Non-existant Data
I'm looking for how to check if a documents exists in my cloud functions My functions belows works fine when just incrementing an existing value, but now I'm trying to add function
Solution 1:
It seems like docRef
either points to a collection or is a query. In that case your snapshot
is of type QuerySnapshot
.
To check if a query has any result, use QuerySnapshot.empty
.
Solution 2:
I kept getting errors saying either empty or exists were not functions (tried many iterations) so eventually I landed on just using an undefined check and it works perfectly.
var db = event.data.ref.firestore;
var docRef = db.collection(userID).doc("joined").collection("total").doc("count");
var getDoc = docRef.get()
.then(snapshot => {
console.log("augu1", snapshot)
if (snapshot._fieldsProto === undefined) {
console.log("digimon1")
docRef.set({
count: 1
});
}
else {
console.log("haha31", snapshot._fieldsProto.count)
var count = Number(jsonParser(snapshot._fieldsProto.count, "integerValue"));
docRef.set({
count: count + 1
});
}
});
Solution 3:
It turns out the problem is much simpler than I imagined: DocumentSnapshot.exists
is a read-only property, not a function. So the proper way to use it is:
if snapshot.exists()
Post a Comment for "Firebase Cloud Functions Check Db For Non-existant Data"