问题描述
我正在尝试在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:
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'));
推荐答案
根据API文档, getAll()看起来像这样:
According to the API docs, the signature for getAll() looks like this:
该参数中的...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 []数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!