简体   繁体   中英

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?

The getDocs() takes a Query as paramater and not an array of DocumentReference . You can map an array ofgetDoc() 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM