user12304080
user12304080

Reputation:

How to check if querySnapshot is empty from firestore

I want to check if the querySnapShot is empty. How can I do this?

Here is my code:

function getOrders(userid) {
  db.collection("users").doc(userid).collection("bestellungen").get().then(function(querySnapshot) {
    if(querySnapshot is empty) {
      console.log("nichts");
    }
    else {
      querySnapshot.forEach(function(doc) {
        console.log(doc.id, " => ", doc.data());
    });
    }

});
}

Looking forward to your answers!

Upvotes: 3

Views: 3841

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83058

You should use the empty property, as follows:

//....
if(querySnapshot.empty) {
  console.log("nichts");
} else {
//....

Upvotes: 10

Related Questions