Firestore getAll() method doesnt accept FirebaseFi

2020-07-11 07:15发布

问题:

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'));

回答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)