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?

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

Read more here:

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: