Telescope144
Telescope144

Reputation: 41

Why might .exists be returning false in Firestore, even if a document does exist?

I am trying to check if a document exists in Firestore - if it already exists, I want to return so that I don't duplicate the entry. I've followed the directions here, which has also been referenced in other answers, but as I step through the program I see "exists" always set to false. As a result, each time this code executes, there is a duplicate entry added. Is there anything I can try to figure out why exists is not set to true when the entry does already exist?

Thanks so much!

    var docRef = db.collection('collection-testing').doc(docName);
    
    docRef.get().then((doc) => {
     let exists = doc.exists;
     if (exists) {
      return;
     }
    });

/* Loads new entry below*/

Upvotes: 0

Views: 733

Answers (1)

Telescope144
Telescope144

Reputation: 41

I've found the issue. My document was only used to store collections, but did not have any data set to the document itself. After adding data in .set(), the above code now functions as expected.

Upvotes: 3

Related Questions