Firestore break out of a query

I have code as below to not to write to DB if the same post has already been posted.

I put a return but self.addtoDB() is still executed. What is the right way to check for an existing post and not write to DB if it exists?

db.collection("posts")
    .where("ytid", "==", self.youtubeId)
    .get()
    .then(function(querySnapshot) {
      querySnapshot.forEach(function(doc) {
        // doc.data() is never undefined for query doc snapshots
        console.log(doc.id, " => ", doc.data());
        alert("This gameplay has already been posted");
        return;
      });
    })
    .catch(function(error) {
      console.log("Error getting documents: ", error);
    });
  self.addToDB();


Read more here: https://stackoverflow.com/questions/64189905/firestore-break-out-of-a-query

Content Attribution

This content was originally published by Daryl Wong at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: