在 Firebase 函数中按 documentId 查询

zel*_*g74 3 javascript firebase google-cloud-functions firebase-admin google-cloud-firestore

当我使用 documentId 作为字段路径从 Firebase Firestore 查询数据时,在网页 (javascript) 和 Firebase Function (Node.js) 上运行脚本时,我得到了不同的行为。

这个javascript给了我完美的结果:

firebase.firestore().collection('test')
  .where(firebase.firestore.FieldPath.documentId(), '<=', 'ccc')
  .get()
  .then(snapshots => { /* results are here */ });
Run Code Online (Sandbox Code Playgroud)

相比之下,Firebase Function (Node.js) 中的相同代码:

admin.firestore().collection('test')
  .where(admin.firestore.FieldPath.documentId(), '<=', 'ccc')
  .get()
  .then(snapshots => { /* ... */ });
Run Code Online (Sandbox Code Playgroud)

给我一个错误:

错误:{ 错误:__name__ 上的过滤器必须是 ClientReadableStream._emitStatusIfDone (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client.js:255:19) 处的文档资源名称 ClientReadableStream._receiveStatus (/user_code) /node_modules/firebase-admin/node_modules/grpc/src/client.js:233:8) 在 /user_code/node_modules/firebase-admin/node_modules/grpc/src/client.js:705:12 代码:3,元数据:元数据 { _internal_repr: {} } }

我使用我自己的文档 ID,我想通过这些 ID 进行查询。我知道我可以通过查询某个文档的内部字段来解决这个问题,但我的问题是:这种不同行为的原因是什么?非常感谢。

我的 firebase 版本是 3.17.4

编辑:此错误已解决,不会出现在 Firebase 3.18.2 版中。

Seb*_*idt 7

这确实是 Node SDK 中的一个功能遗漏,我们将在下一个版本中解决。

您现在可以通过直接传递 DocumentReference 来解决此问题:

const coll = admin.firestore().collection('test')
coll
  .where(admin.firestore.FieldPath.documentId(), '<=', coll.doc('ccc'))
  .get()
  .then(snapshots => { /* ... */ });
Run Code Online (Sandbox Code Playgroud)

  • 抱歉更新此线程,但我无法使用 documentId FieldPath 实现 where 。始终返回“FieldPath.documentId() 的对应值必须是字符串或 DocumentReference”错误:/ 使用字符串或文档(将 `in` 与数组一起使用) (2认同)