【问题标题】:Cloud Functions for Firebase - Error serializing return value:Cloud Functions for Firebase - 序列化返回值时出错:
【发布时间】:2017-12-01 02:38:57
【问题描述】:

我有一个云函数用于交叉引用两个列表并在列表中查找相互匹配的值。该功能似乎工作正常,但是在日志中我一直看到这个 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); 前面,但这仍然给了我错误。不知道我做错了什么以及如何消除错误。感谢您的帮助

【问题讨论】:

    标签: javascript node.js firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    我们通过在链的底部返回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());
        });
    

    【讨论】:

    • 我目前不拥有我正在经历的项目的所有权。其他人可以证实这一点吗?
    【解决方案2】:

    在返回多个作为 Firebase 数据库事务的 Promise 时,我遇到了完全相同的问题。一开始我在打电话:

    return Promise.all(promises);
    

    我的promises 对象是一个数组,我正在使用它来推送所有需要通过调用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(它使用的是循环json库)。

    2017 年 12 月更新: 似乎在最新的 Cloud Functions 版本中,云函数需要返回值(Promise 或值),因此 return; 将导致以下错误:Function returned undefined, expected Promise or value 虽然函数会被执行。因此,当您不返回承诺并且希望云功能完成时,您可以返回一个随机值,例如return true;

    【讨论】:

    • 欢迎来到编程 - 做正常的事情会带来令人着迷和意想不到的方式。老实说,我们应该如何以理智的方式弄清楚这种事情,而不是猜测和检查? FWIW,你的回答解决了我的问题(我用 Firebase “jobs”返回 Promise.all([...])
    • 感谢这个答案很有帮助。在复制一些代码并修复错误后,我错误地使用了 Promise.resolve(array_of_promises):return Promise.all(allPromises).then(() => { return true; });
    【解决方案3】:

    我有相同的错误输出,但设置非常相似,但不知道如何摆脱这个错误。 我不完全确定之前的答案是否已经抓住了每一个本质,所以我把我的解决方案留给你,也许它对你有帮助。

    原来我的代码是这样的:

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

    但是在添加 then 和 catch 之后,错误确实消失了。

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

    【讨论】:

      【解决方案4】:

      试试:

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

      【讨论】:

      • @MikeG 要了解 Promise 以及它们如何与 Cloud Functions 一起使用,请查看包含嵌入式视频的博客:firebase.googleblog.com/2017/06/…
      • @MichaelBleigh 尝试此操作后我仍然遇到同样的错误
      • 我在类似的用例中看到完全相同的错误。在一系列承诺之后,我在数据库引用上调用了一个事务,这些承诺之前包括对同一引用的一次(“值”)读取。该功能似乎具有预期的效果,但我仍然收到错误。
      • 我在同一个俱乐部 - 返回交易给我同样的错误,除了交易处理正确。
      • 如果你这样做return Promise.all([...]).then(function() { return true; });怎么办?
      猜你喜欢
      • 2019-03-02
      • 2017-08-31
      • 2017-10-21
      • 2018-01-03
      • 2021-08-14
      • 2018-04-19
      • 2017-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多