![](/img/trans.png)
[英]Cloud functions are getting called two times, first time with body and second time without
[英]Which function will be called a second time when a firebase transcation fails the first time?
Firebase 文档声称:
如果并发编辑影响事务读取的文档,则调用事务(事务函数)的 function 可能会运行多次。
目前尚不清楚当有并发编辑时,哪个 function 会运行两次。 在下面的示例中, outerFunc
调用了runTranscation
方法,因此文档的字面解释意味着outerFunc
可能被调用了两次。
然而,这似乎是一个奇怪的实现,我怀疑文档的意思是说当并发编辑时innerFunc
可能被调用不止一次。
import { runTransaction } from "firebase/firestore";
function outerFunc () {
console.log('outerFunction');
try {
await runTransaction(db, async function innerFunc(transaction) {
const sfDoc = await transaction.get(sfDocRef);
if (!sfDoc.exists()) {
throw "Document does not exist!";
}
const newPopulation = sfDoc.data().population + 1;
transaction.update(sfDocRef, { population: newPopulation });
});
console.log("Transaction successfully committed!");
} catch (e) {
console.log("Transaction failed: ", e);
}
}
当文档暗示存在并发编辑时,是否可以调用两次outerFunc
?
Firestore SDK 无法调用您的outerFunc
(更不用说多次),但 Firestore SDK 确实可能会多次调用您的innerFunc
回调。
如果您发现文档令人困惑,请在页面本身上提交反馈。 右下角应该有一个链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.