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.
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.
Share Improve this question edited Oct 7, 2017 at 3:20 Dan McGrath 42k11 gold badges103 silver badges130 bronze badges asked Oct 6, 2017 at 13:43 Noman AliNoman Ali 3,34013 gold badges47 silver badges79 bronze badges 1
- @ Noman Ali did you get the solution for these query – Lahari Areti Commented Jul 9, 2018 at 6:29
1 Answer
Reset to default 13I 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);
});