Reputation: 340
firestore database in user
collection:
id1: {
name: 'test',
friends: [
users/id2,
users/id3
]
}
id2: {...}
id3: {...}
I'm trying to receive the snapshots of all references in friends
array. (myRef
si a reference to users/id1
)
const refFriends = (await getDoc(myRef)).data().friends;
const friendsSnaps = await getDocs(refFriends);
console.log(friendsSnaps);
When running, I get this error.
[Unhandled promise rejection: FirebaseError: Expected type 'pc', but it was: an array]
Any ideas?
Upvotes: 1
Views: 33
Reputation: 50850
The getDocs()
takes a Query as paramater and not an array of DocumentReference. You can map an array of getDoc()
from 'friends' array and use Promise.all()
to fetch them at once:
const refFriends = (await getDoc(myRef)).data().friends;
const friendsSnaps = await Promise.all(refFriends.map((f) => getDoc(f)));
Alternatively, you could map array of document IDs and then use in
operator to fetch the documents in a single query but this works only when you have up to 10 friends.
Upvotes: 1