'firebase - get all snapshots from list of refrences
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?
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharmaraj |
