Firestore getAll() method doesnt accept FirebaseFirestore.DocumentReference[] array

I am trying to use getAll() method in a firestore cloud function.
The problem is when I use an array in getAll(), it throws an error:

[ts] Argument of type ‘DocumentReference[]’ is not assignable to parameter of type ‘DocumentReference’.
Property ‘id’ is missing in type ‘DocumentReference[]’.
const questionRefs: FirebaseFirestore.DocumentReference[]

getAll() method looks like, it accepts FirebaseFirestore.DocumentReference[] array, but it doesn’t.
Any idea, where the problem is?

const questionRefs = new Array<FirebaseFirestore.DocumentReference>();
for (const questionID of questions) {
    const ref = admin.firestore().collection('question-bank').doc(questionID)
    questionRefs.push(ref);
}
// then use getAll
admin.firestore().getAll(questionRefs).then(snapshots=>{
    snapshots.forEach(snapshot=>{
        // snapshot.data
        console.log('A');
    })
}).catch(err=>console.log('B'));

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

According to the API docs, the signature for getAll() looks like this:

getAll(…documents) returns Promise containing Array of DocumentSnapshot

That ...documents syntax in the argument is saying that you can pass a bunch of DocumentReference objects separately (as shown in the sample code in the doc). But if you want to pass an array, you have to use the spread operator to convert the array in to individual arguments:

admin.firestore().getAll(...questionRefs)

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply