dontknowhy
dontknowhy

Reputation: 2866

Firestore whereEqualTo only single data

When you get data into querySnapshot using whereEqualTo, it returns only the list type. What if I want to return only a single document? There is the situation in my app that I have to use whereEqualTo to get friend`s UID.

And is there any place to study knowledge of Firestore in addition to GUIDE or Reference? Reference is a little difficult to refer to.

Upvotes: 0

Views: 944

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598765

When you execute a query against Cloud Firestore, there will potentially be multiple documents. So the query snapshot contains a list of those documents. Even if there is only a single matching document, the snapshot will contain a list of one document.

So your code will need to handle that list. This can be very simple, e.g.

...get().then(function(querySnapshot) {
  var document;
  querySnapshot.forEach(function(doc) {
    document = doc;
  });
  if (doc) {
    console.log(doc.data());
  }
})

Upvotes: 1

Related Questions