簡體   English   中英

Firebase的雲功能 - 序列化返回值時出錯:

[英]Cloud Functions for Firebase - Error serializing return value:

我有一個雲函數,用於交叉引用兩個列表,並查找列表中彼此匹配的值。 該函數似乎工作正常,但是在日志中我一直看到此Error serializing return value: TypeError: Converting circular structure to JSON 這是功能......

exports.crossReferenceContacts = functions.database.ref('/cross-ref-contacts/{userId}').onWrite(event => {

    if (event.data.previous.exists()) {
        return null;
    }

    const userContacts = event.data.val();
    const completionRef = event.data.adminRef.root.child('completed-cross-ref').child(userId);
    const removalRef = event.data.ref;

    var contactsVerifiedOnDatabase ={};
    var matchedContacts= {};


    var verifiedNumsRef = event.data.adminRef.root.child('verified-phone-numbers');
    return verifiedNumsRef.once('value', function(snapshot) {

        contactsVerifiedOnDatabase = snapshot.val();

        for (key in userContacts) {
            //checks if a value for this key exists in `contactsVerifiedOnDatabase`
            //if key dioes exist then add the key:value pair to matchedContacts
        };

        removalRef.set(null); //remove the data at the node that triggered this onWrite function
        completionRef.set(matchedContacts); //write the new data to the completion-node

    });

});

我試圖把return前面completionRef.set(matchedContacts); 但這仍然給我錯誤。 不確定我做錯了什么以及如何消除錯誤。 謝謝你的幫助

當返回Firebase數據庫上的多個Promise時,我遇到了完全相同的問題。 起初我打電話給:

return Promise.all(promises);

我的promises對象是我正在使用的數組,我正在通過調用promises.push(<add job here>)來推送所有需要執行的promises.push(<add job here>) 我想這是執行作業的有效方法,因為現在作業將並行運行。

雲功能有效,但我得到了你描述的完全相同的錯誤。

但是,正如邁克爾·布萊(Michael Bleigh)在他的評論中提出的那樣, then添加修復問題並且我不再看到該錯誤:

return Promise.all(promises).then(() => {
  return true;
}).catch(er => {
  console.error('...', er);
});

如果這不能解決您的問題,可能需要將循環對象轉換為JSON格式。 這里寫的是一個例子,但我沒有嘗試過: https//stackoverflow.com/a/42950571/658323 (它使用的是round-json庫)。

更新2017年12月:似乎在最新的Cloud Functions版本中,雲函數將期望返回值(Promise或值),因此return; 將導致以下錯誤: Function returned undefined, expected Promise or value雖然函數將被執行。 因此,當您不返回承諾並且希望雲函數完成時,您可以返回一個隨機值,例如return true;

嘗試:

return verifiedNumsRef.once('value').then(function(snapshot) {
    contactsVerifiedOnDatabase = snapshot.val();

    for (key in userContacts) {
        //checks if a value for this key exists in `contactsVerifiedOnDatabase`
        //if key dioes exist then add the key:value pair to matchedContacts
    };

    return Promise.all([
      removalRef.set(null), //remove the data at the node that triggered this onWrite function
      completionRef.set(matchedContacts)
    ]).then(_ => true);
});

我有一個非常相似的設置相同的錯誤輸出,無法弄清楚如何擺脫這個錯誤。 我不完全確定以前的答案是否已經捕獲了所有的本質,所以我將離開你的解決方案,也許它可以幫到你。

最初我的代碼看起來像這樣:

return emergencyNotificationInformation.once('value', (data) => {
    ...
    return;
});

但在添加之后,趕上錯誤確實消失了。

return emergencyNotificationInformation.once('value')
    .then((data) => {
        ...
        return;
    })
    .catch((error) => {
        ...
        return:
    });
}

我們通過在鏈的底部返回Promise.resolve()修復具有相同錯誤的類似問題,例如:

return event.data.ref.parent.child('subject').once('value')
    .then(snapshot => {
        console.log(snapshot.val());
        Promise.resolve();
    }).catch(error => {
        console.error(error.toString());
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM