node.js - 在 Firebase Firestore 上查询文档时获取空查询
问题描述
我想构建一个 Firebase 函数来检查“朋友”集合中的“计划,==,真”,然后在“文件”集合中使用“fname”创建一个新文档。
我的 Firetore 设置如下:
Users (collection)
User IDs (documents)
|
|
Friends (collection)
Friends (documents with auto IDs)
[fname, scheduled: true/false]
|
|
Filings (collection)
Filings (documents with auto IDs)
每当我运行以下函数时,我都会收到一条错误消息,指出文档为空,即使它不是。我不明白为什么会这样。希望得到您的建议
exports.scheduledFunction = functions.pubsub.schedule('every 5 minutes').onRun(async context => {
const task = await (db.collection('friends').where('scheduled', '==', true).get());
if (task.empty) {
console.log('doc is empty');
return;
}
task.forEach(element => {
console.log(element.id, '==>' , element.data());
});
});
解决方案
您的friends
集合嵌套在users
文档下。您的代码仅访问可能不存在db.collection('friends')
的顶级集合。friends
要访问所有friends
集合,请使用:
db.collectionGroup('friends').where('scheduled', '==', true).get()
推荐阅读
- c++ - 防止 C++ 中的硬编码数字
- automation - 如何使用 Autohotkey 创建三键组合热键?
- spring - 春季侦探行李传播没有传播/工作
- python - 从单行输入创建坐标列表(x、y、z 变量)
- python - 如何在python中将纬度/经度值转换为十进制?
- flutter - 如何使用可滚动小部件和不可滚动小部件制作堆叠小部件
- excel - Reactjs 复制到剪贴板,适用于 Excel 粘贴(react-copy-to-clipboard)
- python - 用确认表有序打印随机列表
- python - 设置新的 docker 镜像然后尝试运行项目后,镜像没有运行
- swift - ISO8601DateFormatter 在 iOS14 和 MacOS 11 中是否损坏?