【发布时间】: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
})
})
})
我想在 onCreate 和 onUpdate 触发器中执行此操作。我应该采取其他方法吗?
更新
不使用事务时错误仍然存在,因此与问题无关。
查询为.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