javascript - 在没有 transaction.get() 的情况下启动 firestore 事务

标签 javascript firebase promise google-cloud-firestore

我想将文档从一个集合移动到另一个集合。因此,我想使用事务来 1. 创建新文档和 2. 删除旧文档。

我可以执行以下操作:

db.runTransaction((transaction) => {
return transaction.get(docRef)
  .then(() => transaction.set(newDocRef.doc(docId), doc))
  .then(() => transaction.delete(docRef));

我如何重写此代码以从 transaction.set() 而不是 transaction.get() 开始,因为我已经有了这个上下文中的文档,所以它是多余的。区别在于 transaction.get() 返回一个 promise ,而 transaction.set() 返回一个 transaction

最佳答案

如果你不想阅读文档,你应该使用 Batched Writes。

Firestore Docs: https://firebase.google.com/docs/firestore/manage-data/transactions

批量写入

If you do not need to read any documents in your operation set, you can execute multiple write operations as a single batch that contains any combination of set(), update(), or delete() operations. A batch of writes completes atomically and can write to multiple documents.

关于javascript - 在没有 transaction.get() 的情况下启动 firestore 事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49252227/

相关文章:

javascript - 使用响应式设计时,呈现适合其容器的最大广告位的最佳方式是什么?

javascript - 如何使用多个 OR 动态构建 MongoDB 查询?

ios - 通知没有到达我的 iOS 应用程序

javascript - 多重依赖 promise 链

javascript - react 父/子状态变化设计问题

javascript - 使用深层链接在 native react 中打开邮件应用程序

swift - 使用规则与前端在 firebase 数据库中进行数据验证有什么区别?

android - Firebase 身份验证有效,但在使用 Google Text to Speech API 时出现错误

javascript - Promise 的 then 方法何时被实现或被拒绝?

javascript - 我可以克隆一个 Promise 吗?