Reputation: 37
db.collection("cities").get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
I'm trying to read multiple document of a collection, but getting error on then. Please help me to sort out this.
Upvotes: 1
Views: 1569
Reputation: 9357
If you want to treat is as a promise, you can convert it to a promise. This isn't a hack, once there's only one emission being produced by get()
. But Firebase expected you to deal with the stream that's coming out from get()
though. Anyway, to convert it to a promise:
db.collection("cities").get().toPromise()
.then((querySnapshot: QuerySnapshot<DocumentData>) => {
querySnapshot.forEach((doc: any) => {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
If you decide to deal with the streaming coming out from get()
, as Firebase API expected you to:
db.collection("cities").get()
.subscribe((querySnapshot: QuerySnapshot<DocumentData>) => {
querySnapshot.forEach((doc: any) => {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
[UPDATE] 1: Based on the docs, there's a new API for getting the snapshots, using the onSnapshot
method:
db.collection("cities")
.onSnapshot((querySnapshot: QuerySnapshot<DocumentData>) => {
querySnapshot.forEach((doc: any) => {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
Upvotes: 4