我正在寻求有关如何在我的 firebase/Angular 6 应用程序中使用事务的帮助。
我有这个:
let transactionResult;
try {
transactionResult = await db.runTransaction(trans => {
console.log('running transaction');
const result = trans.commit();
console.log('result =', result);
return result;
});
} catch (err) {
console.log('catching error; err =', err);
}
console.log('transactionResult =', transactionResult);
我得到这个输出:
running transaction
info: result = Promise { <pending> }
info: catching error; err = { Error: 10 ABORTED: The referenced transaction has expired or is no longer valid.
at Object.exports.createStatusError (/Users/gibranshah/repos/EVABlockchain/functions/node_modules/firebase-admin/node_modules/grpc/src/common.js:87:15)
at Object.onReceiveStatus (/Users/gibranshah/repos/EVABlockchain/functions/node_modules/firebase-admin/node_modules/grpc/src/client_interceptors.js:1188:28)
at InterceptingListener._callNext (/Users/gibranshah/repos/EVABlockchain/functions/node_modules/firebase-admin/node_modules/grpc/src/client_interceptors.js:564:42)
at InterceptingListener.onReceiveStatus (/Users/gibranshah/repos/EVABlockchain/functions/node_modules/firebase-admin/node_modules/grpc/src/client_interceptors.js:614:8)
at callback (/Users/gibranshah/repos/EVABlockchain/functions/node_modules/firebase-admin/node_modules/grpc/src/client_interceptors.js:841:24)
code: 10,
metadata: Metadata { _internal_repr: {} },
details: 'The referenced transaction has expired or is no longer valid.',
note: 'Exception occurred in retry method that was not classified as transient' }
我不熟悉交易如何运作,所以我不知道“引用的交易已过期或不再有效”意味着什么。或“重试方法中发生异常,未归类为 transient ”。 我正在尝试运行尽可能简单的交易。所以我运行 commit() 并返回结果。就是这样。我认为这足够简单,可以工作。
如果我在交易中不执行任何操作,如下所示:
transactionResult = await db.runTransaction(trans => {
console.log('running transaction');
});
我明白了:
info: running transaction
info: catching error; err = Error: You must return a Promise in your transaction()-callback.
at transaction.begin.then (/Users/gibranshah/repos/EVABlockchain/functions/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/index.js:494:32)
at <anonymous>
如果我返回已解决的 promise ,如下所示:
transactionResult = await db.runTransaction(trans => {
console.log('running transaction');
return Promise.resolve();
});
我明白了:
info: running transaction
info: running transaction
info: running transaction
info: running transaction
info: running transaction
info: catching error; err = TypeError: Cannot read property 'seconds' of null
at Function.fromProto (/Users/gibranshah/repos/EVABlockchain/functions/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/timestamp.js:91:47)
at _firestore.request.then.resp (/Users/gibranshah/repos/EVABlockchain/functions/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/write-batch.js:472:42)
at <anonymous>
它似乎重复运行事务,直到它尝试从空对象读取“秒”。
我能编写的最简单且可行的事务是什么?
最佳答案
事实证明,您确实必须至少进行一次读取和一次写入。所以这样做:
transactionResult = await db.runTransaction(trans => {
console.log('running transaction');
const ref = db.collection(colPath).doc(docPath);
return trans.get(ref).then(doc => {
const d = doc.data();
// update data
return trans.set(ref, d);
});
});
...成功了。我正在返回该集的结果,这似乎满足事务(即它提交并且不重复并且没有错误)。
关于angular - 为什么我的 Firebase 交易已过期或不再有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54637861/