【问题标题】:How do I know if my Firestore transaction failed?我如何知道我的 Firestore 交易是否失败?
【发布时间】:2018-07-25 04:33:18
【问题描述】:

我正在使用此代码更新我的文档。

Future<void> save() async {
  print('league save');
  final DocumentReference ref = 
    Firestore.instance.collection('leagues').document(_documentName);
Firestore.instance.runTransaction((Transaction tx) async {
  DocumentSnapshot postSnapshot = await tx.get(ref);
  if (postSnapshot.exists) {
    await tx.update(ref, _getDocument());
    print('league save complete');
  }
});
}

我相信这有时可能会失败,但我不确定。我遇到了一个错误。

我怀疑它有时会失败的原因是因为我的侦听器(在应用程序的其他地方)并不总是在文档更改时被触发。

如何记录或捕获事务中的错误?

【问题讨论】:

    标签: google-cloud-firestore flutter


    【解决方案1】:

    runTransaction 只是一个普通的异步操作,您可以使用 then 和 catchError 来跟进:

    Firestore.instance.runTransaction((Transaction tx) async {
    
      // do whatever      
    
    }).then((val) {
    
     // do something upon success
    
    }).catchError((e) {
    
     // do something upon error 
    
    });
    

    如果你愿意,你可以跳过 .then()

    【讨论】:

      猜你喜欢
      • 2018-11-26
      • 2021-01-18
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多