How do I know if my Firestore transaction failed?

时间:2018-07-25 04:33:18

标签: google-cloud-firestore flutter

I am updating my document with this code.

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');
  }
});
}

I believe that this may be failing sometimes but I am not sure. I am got getting an error.

The reason I suspect it is failing sometimes is because my listener (elsewhere in the app) isn't always getting fired when the document changes.

How do I log or capture an error in the transaction?

1 个答案:

答案 0 :(得分:0)

runTransaction只是一个普通的异步操作,您可以通过then和catchError进行后续操作:

Firestore.instance.runTransaction((Transaction tx) async {

  // do whatever      

}).then((val) {

 // do something upon success

}).catchError((e) {

 // do something upon error 

});

,然后您可以跳过.then()