Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Cloud Firestore query not finding my document

Here's a picture of my data: enter image description here

I'm trying to get that document. This works:

var docRef = db.collection('users').doc('jPDKwHyrFNXNTFI5qgOY');
docRef.get().then(function(doc) {
  if (doc.exists) {
    console.log("Document data:", doc.data());
  } else {
    console.log("No such document!");
  }
}).catch(function(error) {
  console.log("Error getting document:", error);
});

It returns:

enter image description here

I.e., if I know the document's key I can get the document.

This doesn't work:

db.collection('users').where('uid', '==', 'bcmrZDO0X5N6kB38MqhUJZ11OzA3')
.get().then(function(querySnapshot) {
  if (querySnapshot.exists) {
    console.log(querySnapshot.data);
  } else {
    console.log("No such document!");
  }
})
.catch(function(error) {
  console.log("Error getting document: ", error);
});

It just returns No such document! What's wrong with my query?

like image 621
Thomas David Kehoe Avatar asked Oct 11 '17 16:10

Thomas David Kehoe


People also ask

How do I access firestore documents?

There are three ways to retrieve data stored in Cloud Firestore. Any of these methods can be used with documents, collections of documents, or the results of queries: Call a method to get the data once. Set a listener to receive data-change events.

How do I get the document ID added to firestore?

When you call the . add method on a collection, a DocumentReference object is returned. DocumentReference has the id field, so you can get the id after the document was created. // Add a new document with a generated id.

How do I use queries in Cloud Firestore?

In Cloud Firestore, you can use queries to retrieve individual, specific documents or to retrieve all the documents in a collection that match your query parameters. Your queries can include multiple, chained filters and combine filtering and sorting.

What is Cloud Firestore's NoSQL data model?

Following Cloud Firestore's NoSQL data model, you store data in documents that contain fields mapping to values. These documents are stored in collections, which are containers for your documents that you can use to organize your data and build queries.

What's new in Cloud Firestore?

This means you can now query, for example, all documents in a "Projects" collection where the project's status field is not equal to the value "completed" On a similar note, Cloud Firestore also supports not-in queries, where you can query for documents where fields are not in a list of values.

Is it possible to create indexes in FireStore?

This is not possible in Firestore currently. We should understand why. In Firestore, all queries are served off indexes for performance reasons. Most of these indexes on single columns are automatically created for us, which is why when we get started, we don’t realise it.


1 Answers

The difference in your two requests is that in the first case you are retrieving one document which gives you a DocumentSnapshot which has the exists property and the data() method.

In the second case you do a query, which gives you a QuerySnapshot that has to be handled differently from a DocumentSnapshot. Instead of a single document you get a list/collection of documents. You can check if data has been retrieved using the empty or size properties, and then go through the results using the forEach method or going through the docs array:

db.collection('users').where('uid', '==', 'bcmrZDO0X5N6kB38MqhUJZ11OzA3')
.get().then(function(querySnapshot) {
  if (querySnapshot.size > 0) {
    // Contents of first document
    console.log(querySnapshot.docs[0].data());
  } else {
    console.log("No such document!");
  }
})
.catch(function(error) {
  console.log("Error getting document: ", error);
});
like image 188
Scarygami Avatar answered Nov 14 '22 21:11

Scarygami