【问题标题】:Cloud firestore trigger query by documentID for array of idsCloud Firestore 触发器通过 documentID 查询 id 数组
【发布时间】:2020-03-22 20:31:42
【问题描述】:

我正在尝试编写一个事务,该事务首先通过 documentId 从 id 列表中查询文档,然后进行一些更新。

我收到错误:

FieldPath.documentId() 的对应值必须是字符串或 DocumentReference。

例如:

const indexArray = [..list of doc ids...]
const personQueryRef = db.collection("person").where(admin.firestore.FieldPath.documentId(), "in", indexArray)

return db.runTransaction(transaction => {
  return transaction.get(personQueryRef).then(personQuery => {
    return personQuery.forEach(personRef => {
      transaction.update(personRef, { ...update values here })
      //more updates etc
    })
  })
})

我想在 onCreateonUpdate 触发器中执行此操作。我应该采取其他方法吗?

更新

不使用事务时错误仍然存​​在,因此与问题无关。

查询为.where(admin.firestore.FieldPath.documentId(), "==", "just_one_doc_id") 时不会出现该问题。所以,问题在于使用FieldPath.documentId()in

【问题讨论】:

  • 我确定这与 Cloud Functions 或您使用的触发器类型没有任何关系。你确定这与交易有关吗?如果只执行personQueryRef 查询而不执行事务怎么办?
  • 另外我建议您将该错误消息粘贴到网络搜索中 - 还有很多其他问题报告了同样的事情。
  • @DougStevenson 我已经将我的函数简化为不使用事务,但它仍然以同样的方式出错。当我执行admin.firestore.FieldPath.documentId(), "==", "just_one_doc_id" 时不会发生错误。是否有另一种方法可以为一组 id 实现这一目标?其他堆栈溢出/结果似乎不能很好地回答这个问题。
  • 我可以进行多个查询并加入结果 - 这是更好的方法吗?

标签: node.js firebase google-cloud-firestore


【解决方案1】:

听起来 SDK 不支持您尝试执行的查询类型。是不是故意的,我不知道。但是,如果您想处理多个文档,并且您已经知道它们的所有 ID,则可以改用 getAll(...)

// build an array of DocumentReference objects
cost refs = indexArray.map(id => db.collection("person").doc(id))

return db.runTransaction(transaction => {
  // pass the array to getAll()
  return transaction.getAll(refs).then(docs => {
    docs.forEach(doc => {
      transaction.update(doc.ref, { ...update values here })
    })
  })
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2021-09-11
    相关资源
    最近更新 更多