云函数错误:将圆形结构转换为JSON

时间:2019-12-09 18:59:51

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

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

错误:

Firebase Error report

这是云功能代码

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

这似乎很好。

如何将对象作为参数接收?

1 个答案:

答案 0 :(得分:1)

您没有正确使用可调用类型函数。从documentation可以看到,无论从应用程序传递什么,传递给SDK的函数始终会收到两个参数datacontext。您从应用程序传递的单个对象将成为单个 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的值,则可能更容易找出。