繁体   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