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

标签 node.js firebase google-cloud-firestore

我正在尝试在 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/

相关文章:

javascript - 使用 Jade 时出现意外文本错误

node.js - order-update.js 发送 "{}"作为事务 api actions-on-google 中的响应

javascript - Firebase 可调用函数响应不起作用

sql - 在google cloud firestore中执行JOIN查询

node.js - 为什么 firestore 文档不创建也不更新数据?

javascript - 无法通过 key (Firestore)获取数据

node.js - SailsJS 使用一个 Controller 操作调用两个 Service 方法

javascript - reactjs - 如何从react js中的map方法中仅访问一项

android - Firebase 的 Cloud Firestore 免费配额一旦超过就永久用完?

javascript - 刷新页面时Socket.io多个连接