javascript - Firestore如何停止事务?

标签 javascript firebase google-cloud-firestore

在事务中,如果数据不存在,我只想写入数据

DocumentReference callConnectionRef1 = firestore.collection("connectedCalls").document(callChannelModel.getUid());

  firestore.runTransaction(new Transaction.Function < Void > () {
 @Override
 public Void apply(Transaction transaction) throws FirebaseFirestoreException {
  DocumentSnapshot snapshot = transaction.get(callConnectionRef1);

   Log.d(TAG, callChannelModel.getUid());


  if (!snapshot.exists()) {
   //my code
   transaction.set(callConnectionRef1, model);
  } else {
   //do nothing   
  }
  return null;

 });

您可以在我的文档中看到引用是基于 uid 的,并且在我的日志中我正在打印 uid

因此,如果 uid 的数据不存在,我的日志仅打印一次,并且我在其他地方调用 transaction.set() ,它会继续显示数据已经存在的 uid 日志,如果我不调用 transaction.set( )

我怎样才能阻止它。

最佳答案

我在 Android 上也遇到过这种情况。事务执行 5 次尝试应用自身,然后才会调用 onFailure() 函数(即使您在 apply() 函数中抛出异常)。

但看起来这是预期的行为:

  1. 请参阅此处:https://googleapis.dev/python/firestore/latest/transaction.html
    请注意,MAX_ATTEMPTS 默认为 5。
  2. 在此 github 问题中 https://github.com/firebase/firebase-js-sdk/issues/520有人请求添加“重试次数”选项。

关于javascript - Firestore如何停止事务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50735358/

相关文章:

node.js - Firestore 中什么算作读取操作?

javascript - 如何在 Firebase Firestore 中找到文档的父集合?

android - 不支持通用通配符类型

javascript - 收到错误拒绝在框架中显示 'http://www.asp.net/',因为它使用 iframe-resizer 将 'X-Frame-Options' 设置为 'SAMEORIGIN'

带有 ajax 调用的 Javascript 循环生成相同的 JSON?

javascript - 鼠标悬停(显示内容)javascript

android - 如何在 Android Studio 的 Firebase 数据库中检索 1 个 child 下的数据列表

android - Firebase Auth Ui Google 登录 "Developer Error"

javascript - 将大 html 文件附加到模态窗口

firebase - 从Firebase获取用户数据