javascript - 从 ID 数组中查询 firestore 中的文档
问题描述
我想知道如何从一组 ID 的 firestore 集合中查询文档?我只想要集合中 ID 数组中的文档。我查看了另一个答案,认为我的方法是正确的,但是,我遇到了错误。
(node:15105) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'data' of undefined
> at /Users/username/SideProjects/projectname/functions/index.js:40:38
> at processTicksAndRejections (internal/process/task_queues.js:95:5)
发生错误是因为该函数未从该 ID 数组的集合中找到任何文档。但是,我仔细检查了数据库,并知道集合中有文档具有数组中的 ID。
const admin = require('firebase-admin')
....
let feedItems = db.collection(feedItemsCollection)
feedItemsList = feedItems.where(admin.firestore.FieldPath.documentId(), 'in', ['HPOorsSnbHpTYwwXxfWw']).get().then(snapshot2 => {
console.log(admin.firestore.FieldPath.documentId())
console.log("In feed Items")
//console.log(feedItemIds)
console.log(snapshot2[0])
//error happens on this line because snapshot2[0] returns undefined
console.log(snapshot2[0].data())
})
Snapshot2[0] 返回 undefined 我假设这意味着没有返回数据。我认为我没有正确调用 documentId(),但不知道解决方法。
解决方案
您的代码“可能”存在两个问题。遵循这两点以确保一切正常
- 里面的数据
snapshot2
可能empty
你首先必须修复你的代码来测试这个理论。您没有snapshot2
正确访问数据。要做到这一点,一种方法是:
// `snapshot2` will have a `docs` property that you can leverage
const snapshot2Data = snapshot2.docs.map((doc) => doc.data());
.documentId()
可能没有做它应该做的(如你所说)
为了检验这个理论,检查是否snapshot2Data
为空。跑 :
console.log(snapshot2Data); // what do you get ?
如果不是,它不是空的并且你得到了数据,那么你就准备好了。无事可做
如果是,则为空,然后运行:
console.log(admin.firestore.FieldPath.documentId()); // what do you get ?
你回来了string
吗?如果不是,那么我们还有另一个问题。您还需要仔细查看您的firebase-admin
设置。
推荐阅读
- c++ - 因此,如果我不需要 emplace_back (emplace) 提供的新功能,我应该使用 push_back (insert)?
- c# - 使用 ajax 和 SQL 存储过程将数据加载到 html 表中
- php - 苗条的异常不知道出了什么问题
- spring-boot - 如果该键的值为null,如何在springboot中使用mapstruct隐藏有效载荷中的键
- kotlin - 将具有不同方法签名的不同函数作为kotlin中的参数传递给函数
- python - 访问属性对象(用于替换 setter 方法)
- oracle - Oracle 12c 启动错误:ORA-00093: _shared_pool_reserved_min_alloc 必须介于 4000 和 0 之间
- angular - Angular Firestore - 获取文档 ID 作为属性以在其他功能中再次使用它 - 打字稿
- java - 基于实体名称及其主键的通用数据获取 API
- r - 为什么在 data.table 格式中运行代码两次或三次时,分配给引用 (:=) 会给出不同的结果?