【问题标题】:Cloud functions error: Converting circular structure to JSON云函数错误:将循环结构转换为 JSON
【发布时间】:2019-12-09 18:59:51
【问题描述】:

尝试使用 Firebase 的管理 SDK 在 Firebase Cloud Functions 中设置自定义声明。问题似乎是我传递给函数的声明对象。我了解什么是圆形对象结构,但我不确定为什么会在这里发生。

错误:

这里是云功能代码

exports.setCustomClaims2 = functions.https.onCall((uid, claims) => {
    return admin.auth().setCustomUserClaims(uid,claims).then(() => {
            return {
                message: `Success! User updated with claims`
            }
        })
        .catch(err => {
            return err;
        })
});

这是调用它的前端代码:

let uid = "iNj5qkasMdYt43d1pnoEAIewWWC3";
let claims = {admin: true};

const setCustomClaims = firebase.functions().httpsCallable('setCustomClaims2');
setCustomClaims(uid,claims)

有趣的是,当我像这样直接在云函数调用中替换claims参数时

admin.auth().setCustomUserClaims(uid,{admin: true})

这似乎工作得很好。

对象作为参数被接收的方式有什么不同吗?

【问题讨论】:

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


    【解决方案1】:

    您没有正确使用可调用类型函数。从documentation 可以看出,无论您从应用程序传递什么,您传递给 SDK 的函数始终接收两个参数,datacontext。您从应用程序传递的 single 对象成为 single data 参数。您不能传递多个参数,并且该参数不会被分解为多个参数。

    您应该做的是将 uid 和声明组合成一个对象,然后传递它:

    setCustomClaims({ uid, claims })
    

    然后在函数中将其作为单个参数接收:

    exports.setCustomClaims2 = functions.https.onCall((data, context) => {
        // data here is the single object you passed from the client
        const { uid, claims } = data;
    })
    

    我会注意到,在函数中使用 console.log 将帮助您调试函数正在执行的操作。如果您记录了uidclaims 的值,这可能会更容易弄清楚。

    【讨论】:

    • 非常感谢!现在这很有意义:)
    猜你喜欢
    • 2019-01-31
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 2018-09-17
    • 2018-06-08
    • 2021-10-23
    相关资源
    最近更新 更多