Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Firestore collections queries not working

As Cloud Firestore is new, I am having problems using it.

I have to get Collection of all users and traverse it. But it is not working.

db.collection("users").get().then(function(querySnapshot){
      console.log(querySnapshot.data());
});

It says:

querySnapshot.data is not a function

And following code:

callFireBase(mobileToCheck){
        db.collection("users").where("mobile_no", '==', mobileToCheck).get().then(function(querySnapshot){
            if (querySnapshot.exists) {
                var userData = querySnapshot.data();
                var userId = querySnapshot.id;
                console.log(mobileToCheck + "Exist In DB");
            }else{
                console.log(mobileToCheck + "Do Not Exist In DB");
            }
        });
}

Is always printing

923052273575 Do Not Exist In DB

Even if it exists, See following image for reference.

enter image description here

like image 403
Noman Ali Avatar asked Oct 06 '17 13:10

Noman Ali


People also ask

Can you query firestore?

Cloud Firestore provides powerful query functionality for specifying which documents you want to retrieve from a collection or collection group. These queries can also be used with either get() or addSnapshotListener() , as described in Get Data and Get Realtime Updates.

Can you use SQL in firestore?

FireSQL is a library built on top of the official Firebase SDK that allows you to query Cloud Firestore using SQL syntax. It's smart enough to issue the minimum amount of queries necessary to the Firestore servers in order to get the data that you request.

How many documents can a collection hold firestore?

10 for single-document requests and query requests. 20 for multi-document reads, transactions, and batched writes. The previous limit of 10 also applies to each operation.


1 Answers

I think you have some things confused as querySnapshot doesn't have data, but it does have docs which have data.

In your first example, you are asking it to return all documents in the collection. You'll want something like this instead:

db.collection("users").get().then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
        console.log(doc.id, " => ", doc.data());
    });
});

Key difference is looping over the docs in querySnapshot and console logging the data from each doc.

For your second example, you'll want to check if the querySnapshot is empty, rather than checking if it exists.

db.collection("users").where("mobile_no", "==", mobileToCheck)
.get()
.then(function(querySnapshot) {
    if (querySnapshot.exists) {
        querySnapshot.forEach(function(doc) {
            console.log(doc.id, " => ", doc.data());
            var userData = doc.data()
            var userId = doc.id
            console.log(mobileToCheck + "Exist In DB");
        });
    } else {
        console.log(mobileToCheck + "Do Not Exist In DB");
    };
})
.catch(function(error) {
    console.log("Error getting documents: ", error);
});
like image 190
Dan McGrath Avatar answered Oct 05 '22 11:10

Dan McGrath