Reputation: 953
I've been trying to figure out how to query a Firebase database for a while, but to no avail. Here's what I have right now:
async query(){
const bar = firebase.firestore().collection('foo');
const res = await bar.orderBy('gopher').limit(20).get();
}
res
isn't the actual data, but rather some sort of internal object:
{
o_: {
J_ : {
fromCache: false,
ne: true,
...
}
},
q_ : {
...
}
}
For the data structure, I have a collection called foo
, and multiple documents with unique ids - which each have a numerical field called gopher
. I'm trying to sort by gopher
and get 20 documents at the top, but res.data()
is not a function and res.data
is undefined.
Upvotes: 0
Views: 1117
Reputation: 317352
res
is a QuerySnapshot type object. If you want to see the results of the query, you can get the raw data using the pattern described in the documentation. I've renamed the variables to be more descriptive.
const fooCollection = firebase.firestore().collection('foo');
const querySnapshot = await fooCollection.orderBy('gopher').limit(20).get();
if (querySnapshot.size > 0) {
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
}
else {
console.log("No documents in the results");
}
Upvotes: 2
Reputation: 1161
It should be res.docs
not res.data
res.docs
contains array firestore document snapshot.
To print all the data
console.log(res.docs.map(snap=>snap.data()));
Upvotes: 0