我正在用这段代码更新我的文档。
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');
}
});
}
我相信这有时可能会失败,但我不确定。我遇到了错误。
我怀疑它有时会失败的原因是因为我的监听器(在应用程序的其他地方)在文档更改时并不总是被触发。
如何记录或捕获交易中的错误?
最佳答案
runTransaction 只是一个普通的异步操作,您可以跟进 then 和 catchError:
Firestore.instance.runTransaction((Transaction tx) async {
// do whatever
}).then((val) {
// do something upon success
}).catchError((e) {
// do something upon error
});
如果你愿意,你可以跳过 then.then()
关于google-cloud-firestore - 我如何知道我的 Firestore 交易是否失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51510846/