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.
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.
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.
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.
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With