Reputation: 978
I'm trying to get all the documents from a collection to display in a react component, I would like to put all the docs in an array I've tried doing this :
export const getProjects = async () => {
firestore
.collection("projects")
.get()
.then((querySnapshot) => {
return querySnapshot.docs;
})
.catch((e) => {
console.error(e);
});
};
but I get undefined, if I do a console.log(querySnapshot.docs) it works.
Upvotes: 0
Views: 42
Reputation: 4332
You're not returning firestore.collection(...)
from the getProjects
function, you're only returning it from inside then
callback function.
Return firestore.collection(...)
from the getProjects
like this
export const getProjects = async () => {
return firestore
.collection("projects")
.get()
.then((querySnapshot) => {
return querySnapshot.docs;
})
.catch((e) => {
console.error(e);
});
};
Upvotes: 1