Cloud functions error: Converting circular structure to JSON

Cloud functions error: Converting circular structure to JSON

尝试使用 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})

这似乎工作得很好。

接收对象作为参数的方式有区别吗?

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

您应该做的是将 uid 和 claims 合并到一个对象中,然后传递它:

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 的值,这可能更容易弄清楚。