简体   繁体   English

Firebase的云功能 - 序列化返回值时出错:

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

I have a Cloud Function used to cross reference two lists and find values that match each other across the lists. 我有一个云函数,用于交叉引用两个列表,并查找列表中彼此匹配的值。 The function seems to be working properly, however in the logs I keep seeing this Error serializing return value: TypeError: Converting circular structure to JSON . 该函数似乎工作正常,但是在日志中我一直看到此Error serializing return value: TypeError: Converting circular structure to JSON Here is the function... 这是功能......

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

    });

});

I tried putting return in front of completionRef.set(matchedContacts); 我试图把return前面completionRef.set(matchedContacts); but that still gives me the error. 但这仍然给我错误。 Not sure what I am doing wrong and how to rid the error. 不确定我做错了什么以及如何消除错误。 Thanks for your help 谢谢你的帮助

I was having the exact same issue when returning multiple promises that were transactions on the Firebase database. 当返回Firebase数据库上的多个Promise时,我遇到了完全相同的问题。 At first I was calling: 起初我打电话给:

return Promise.all(promises);

My promises object is an array that I'm using where I'm pushing all jobs that need to be executed by calling promises.push(<add job here>) . 我的promises对象是我正在使用的数组,我正在通过调用promises.push(<add job here>)来推送所有需要执行的promises.push(<add job here>) I guess that this is an effective way of executing the jobs since now the jobs will run in parallel. 我想这是执行作业的有效方法,因为现在作业将并行运行。

The cloud function worked but I was getting the exact same error you describe. 云功能有效,但我得到了你描述的完全相同的错误。

But, as Michael Bleigh suggested on his comment, adding then fixed the issue and I am no longer seeing that error: 但是,正如迈克尔·布莱(Michael Bleigh)在他的评论中提出的那样, then添加修复问题并且我不再看到该错误:

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

If that doesn't fix your issue, maybe you need to convert your circular object to a JSON format. 如果这不能解决您的问题,可能需要将循环对象转换为JSON格式。 An example is written here, but I haven't tried that: https://stackoverflow.com/a/42950571/658323 (it's using the circular-json library). 这里写的是一个例子,但我没有尝试过: https//stackoverflow.com/a/42950571/658323 (它使用的是round-json库)。

UPDATE December 2017: It appears that in the newest Cloud Functions version, a cloud function will expect a return value (either a Promise or a value), so return; 更新2017年12月:似乎在最新的Cloud Functions版本中,云函数将期望返回值(Promise或值),因此return; will cause the following error: Function returned undefined, expected Promise or value although the function will be executed. 将导致以下错误: Function returned undefined, expected Promise or value虽然函数将被执行。 Therefore when you don't return a promise and you want the cloud function to finish, you can return a random value, eg return true; 因此,当您不返回承诺并且希望云函数完成时,您可以返回一个随机值,例如return true;

Try: 尝试:

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);
});

I had the same error output with a pretty similar setup and couldn't figure out how to get rid of this error. 我有一个非常相似的设置相同的错误输出,无法弄清楚如何摆脱这个错误。 I'm not totally sure if every essence has been captured by the previous answers so I'm leaving you my solution, maybe it helps you. 我不完全确定以前的答案是否已经捕获了所有的本质,所以我将离开你的解决方案,也许它可以帮到你。

Originally my code looked like this: 最初我的代码看起来像这样:

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

But after adding then and catch the error did go away. 但在添加之后,赶上错误确实消失了。

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

We fixed a similar issue with the same error by returning Promise.resolve() at the bottom of the chain, eg: 我们通过在链的底部返回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