Hi I'm starting with javascript and react-native and I'm trying to figure out this problem for hours now. Can someone explain me how to get all the documents from firestore collection ?
I have been trying this:
async getMarkers() {
const events = await firebase.firestore().collection('events').get()
.then(querySnapshot => {
querySnapshot.docs.map(doc => {
console.log('LOG 1', doc.data());
return doc.data();
});
});
console.log('LOG 2', events);
return events;
}
Log 1 prints all the objects(one by one) but log 2 is undefined, why ?
I prefer to hide all code complexity in my services... so, I generally use something like this:
In my events.service.ts
In my sth.page.ts
Enjoy :)
I made it work this way:
You could get the whole collection as an object, rather than array like this:
That would give you a better representation of what's in firestore. Nothing wrong with an array, just another option.
The example in the other answer is unnecessarily complex. This would be more straightforward, if all you want to do is return the raw data objects for each document in a query or collection:
if you need to include the key of the document in the response, another alternative is:
if you want include Id
Same way with array