node.js - 写入操作如何在 Firebase RTDB 中工作,即使在云函数超时后也会执行
问题描述
几个月以来,当我们在 firebase RTDB 中读/写时,我们的许多云功能都面临超时。
我们注意到,当我们从 RTDB 读取数据和函数超时时,函数执行完成。但是当我们在 RTDB 中写入数据并且函数超时时,即使函数超时,几分钟后也会发生写入操作。不知道为什么以及如何发生这种情况。写操作是否在队列中添加了一些位置?firebase RTDB 写操作如何与 firebase CF 配合使用。
下面添加代码片段以供参考。
exports.manager = functions.database.instance(instanceName11).ref('/A/sk').onCreate((snapshot, context) => {
return managerHandler(snapshot, context, false);
});
function managerHandler(snapshot, context, onUpdate) {
const skipData = snapshot.val();
// if (printDebugLogs)
console.log('skipData: ', skipData);
// console.log('onUpdate ',onUpdate)
if (skipData) {
// const skip = skipData.split("_");
const player = skipData;
const mNodeRef = snapshot.ref.parent.once("value")
const cardsRef = snapshot.ref.parent.parent.parent.child("P").child(player).child("c").once("value")
return Promise.all([cardsRef, mNodeRef]).then(snapResults => {
var cards = snapResults[0].val()
const bid = snapResults[1].val()
// if (printDebugLogs)
{
console.log('bid : ', bid);
console.log('cards: ', cards);
}
var d = bid.d;
const p1Card = d != null ? d.i : null
const p2Card = d != null ? d.j : null
const p3Card = d != null ? d.k : null
const p4Card = d != null ? d.l : null
var bidValue;
if (bid.u == "-") {
bidValue = parseInt(getBidForPlayer(getCardsAsList(cards)));
if (bidValue < 1) {
bidValue = 1;
}
var mergedUpdate = {};
mergedUpdate['/sk'] = null;
mergedUpdate['/z'] = 1;
return snapshot.ref.parent.update(mergedUpdate, (error) => {
if (error) {
logError(error);
} else {
console.log("bidSkipManagerHandler ", 'Data saved successfully.');
}
});
} else {
return true;
}
});
} else {
return true;
}
}
更新操作发生甚至超时
解决方案
最有可能的是,写操作已经发送到服务器,并且在函数终止时还没有被客户端确认。
除此之外,虽然我认为这与问题无关,但我建议使用Promise或Firebase的经典回调,但不要同时使用两者。所以那是:
return snapshot.ref.parent.update(mergedUpdate).then((error) => {
if (error) {
logError(error);
} else {
console.log("bidSkipManagerHandler ", 'Data saved successfully.');
}
return true;
});