Firestore getAll()方法不接受FirebaseFirestore.DocumentReference []数组

meh*_*met 4 node.js firebase google-cloud-firestore

我试图在firestore云函数中使用getAll()方法.问题是当我在getAll()中使用数组时,它会抛出一个错误:

[ts]类型'DocumentReference []'的参数不能分配给'DocumentReference'类型的参数.'DocumentReference []'类型中缺少属性'id'.const questionRefs:FirebaseFirestore.DocumentReference []

getAll()方法看来,它接受FirebaseFirestore.DocumentReference []数组,但它不接受.任何想法,问题出在哪里?

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'));
Run Code Online (Sandbox Code Playgroud)

Dou*_*son 11

根据API文档,getAll()的签名如下所示:

getAll(... documents)返回包含DocumentSnapshot数组的Promise

...documents在参数语法是说,可以分别通过一串DocumentReference的物体(如图在doc示例代码).但是如果要传递数组,则必须使用spread运算符将数组转换为单个参数:

admin.firestore().getAll(...questionRefs)
Run Code Online (Sandbox Code Playgroud)