I have the following code, I want to fetch data from two collections of firestore,the first element of each collection data. What reason data is not being returned ?
const myFunction = async () => {
const usersData = await db.collection("users").get()
const productsData = await db.collection("products").get()
return [usersData[0], productsData[0]]
}
2
Answers
Try this solution
You are using
get()
on a CollectionReference that’ll fetch all the N documents present in that collection and you’ll be charged for N reads. Instead, you should be using a query with limit 1 that’ll only fetch the first document ordered by the constraints that you specify as shown below:The queries above will only cost 2 reads (1 from each collection).