Firestore的getAll()方法不接受FirebaseFirestore.DocumentReference []数组 [英] Firestore getAll() method doesnt accept FirebaseFirestore.DocumentReference[] array
问题描述
我正在尝试在Firestore云函数中使用getAll()方法. 问题是当我在getAll()中使用数组时,会引发错误:
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]类型'DocumentReference []'的参数不能分配给'DocumentReference'类型的参数. 类型'DocumentReference []'中缺少属性'id'. constquestRefs:FirebaseFirestore.DocumentReference []
[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 []数组,但不接受.
任何想法,问题出在哪里?
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'));
推荐答案
According to the API docs, the signature for getAll() looks like this:
getAll(... documents)返回包含DocumentSnapshot数组的Promise
getAll(...documents) returns Promise containing Array of DocumentSnapshot
该参数中的...documents
语法表示您可以分别传递一堆DocumentReference对象(如doc中的示例代码所示).但是,如果要传递数组,则必须使用 spread运算符将数组转换为单个参数:
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)
这篇关于Firestore的getAll()方法不接受FirebaseFirestore.DocumentReference []数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!