我正在尝试在 firestore 云函数中使用 getAll() 方法。 问题是当我在 getAll() 中使用数组时,它会抛出一个错误:
[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()
方法看起来像,它接受 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'));
最佳答案
根据 API 文档,getAll() 的签名看起来像这样:
getAll(...documents) returns Promise containing Array of DocumentSnapshot
参数中的 ...documents
语法表示您可以单独传递一堆 DocumentReference 对象(如文档中的示例代码所示)。但是如果你想传递一个数组,你必须使用 spread operator将数组转换为单个参数:
admin.firestore().getAll(...questionRefs)
关于node.js - Firestore getAll() 方法不接受 FirebaseFirestore.DocumentReference[] 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49776395/