這個問題有點類似于Firebase事務回傳null并且完成時沒有錯誤。 然而,我已經有了建議的解決方案。
下面使用Firebase web版本9。運行于node v14.17.3
const returnVal = await runTransaction(firebaseRef, async node => {
console.log("1"/span>, node)。
if (node && node.ready != true) {
node.ready = true;
}
console.log("2"/span>, node)。
return node;
});
if (! returnVal. committed) throw new Error("not committed" )。
console.log("3"/span>, returnVal. snapshot.toJSON()。
在你繼續說之前,我沒有把節點設定為一個空物件,我希望它以后已經成為一個物件。
這是我的輸出。
1 null
2 null
1 {otherMeta: {...}}
2 {otherMeta: {...}, ready: true}。
3 null
正如你所看到的,交易完成后是一個空值。這很奇怪。
uj5u.com熱心網友回復:
我正準備提交一個針對SDK的回歸錯誤,當我注意到這似乎來自于async關鍵字。
如果我使用這樣的代碼:
runTransaction(firebaseRef, async (node) => {
console.log("1"/span>, node)。
if (node && node.ready != true) {
node.ready = true;
}
console.log("2"/span>, node)。
return node;
}).then((result) =>/span> {
if (! result. committed) throw new Error("not committed") 。
console.log("3", result.snapshot.toJSON() )。)
});
我得到的最后一條日志的輸出為:
3 null
當我洗掉僅async關鍵字(反正不需要),我得到:
3 {a: true, ready: true}
我的測驗床。https://stackblitz.com/edit/firebase-v9-rtdb
我不知道async關鍵字是什么導致的。盡管如前所述,這個關鍵詞在我的代碼段中是不必要的,但在你的代碼中是需要的,我不知道它如何影響結果。
uj5u.com熱心網友回復:
TL;DR:只需洗掉async關鍵字。
Frank一針見血地指出,但我要寫下一個單獨的答案,以便我可以使用語法高亮。如果你有兩個相同的函式:
function theAnswer() { return 42; }
async function theEventualAnswer() { return 42; }
這兩個函式的行為非常不同。第一個函式回傳字面意思42。第二個函式實際上回傳Promise.resolve(42)。如果你嘗試添加theAnswer() 1,你會得到43。如果你試圖添加theEventualAnswer() 1,你會得到'[object Promise]1'.
在這種情況下,你已經回傳了Promise.resolve({...node, ready: true}),SDK后來試圖將其變成一個參考,并得到了一些非常奇怪的結果,因為它期望得到具體型別。
我已經代表您提交了一個錯誤,詳細說明了團隊如何向您提供一個編譯時錯誤資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/322228.html
標籤:
上一篇:如何使用node.js發送短信
下一篇:為什么FileAge回傳意外值?
