我正在寻找有关如何在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' }
我不熟悉事务的工作原理,因此我不知道“所引用的事务已过期或不再有效。”或“重试方法中发生异常,但未归类为瞬态”。 我正在尝试尽可能简单地进行交易。所以我运行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>
如果我按以下方式返回已解决的承诺:
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>
似乎重复运行该事务,直到尝试从空对象读取“秒”。
我能编写的最简单的交易是什么?
答案 0 :(得分:1)
您应该在开始处添加返回
return db.runTransaction(trans => {
console.log('running transaction');
});
答案 1 :(得分:0)
事实证明,您必须至少进行一次读取和一次写入。这样做:
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);
});
});
...可以解决问题。我正在返回集合的结果,这似乎满足了事务要求(即,它提交并且不重复并且没有错误)。