简体   繁体   中英

Cloud functions error: Converting circular structure to JSON

Trying to use the admin SDK from Firebase to set custom claims within Firebase Cloud Functions. The issue seems to be the claims object I pass into the function. I understand what a circular object structure is, but I'm not sure why it is happening here.

The Error:

Firebase 错误报告

Here is the cloud function code

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

And here is the front end code to call it:

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

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

What is interesting is that when I replace the claims parameter directly in the cloud function call like so

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

This seems to work just fine.

Is there a difference in how the object gets received as a parameter?

You're not using the callable type function correctly. As you can see from the documentation , the function you pass to the SDK always receives two arguments, data and context , no matter what you pass from the app. A single object you pass from the app becomes the single data parameter. You can't pass multiple parameters, and that parameter doesn't get broken up into multiple parameters.

What you should do instead is combine uid and claims into a single object, and pass it:

setCustomClaims({ uid, claims })

Then receive it as a single parameter in the function:

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

I'll note that using console.log in the function will help you debug what your function is doing. If you logged the values of uid and claims , this probably would have been easier to figure out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM