Javascript 云函数错误:将循环结构转换为JSON
尝试使用Firebase的admin SDK在Firebase云函数中设置自定义声明。问题似乎是我传递给函数的索赔对象。我理解什么是圆形对象结构,但我不确定为什么会发生在这里 错误: 下面是云函数代码Javascript 云函数错误:将循环结构转换为JSON,javascript,node.js,firebase,google-cloud-functions,firebase-admin,Javascript,Node.js,Firebase,Google Cloud Functions,Firebase Admin,尝试使用Firebase的admin SDK在Firebase云函数中设置自定义声明。问题似乎是我传递给函数的索赔对象。我理解什么是圆形对象结构,但我不确定为什么会发生在这里 错误: 下面是云函数代码 exports.setCustomClaims2 = functions.https.onCall((uid, claims) => { return admin.auth().setCustomUserClaims(uid,claims).then(() => {
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)
有趣的是,当我直接在cloud函数调用中替换claims参数时
admin.auth().setCustomUserClaims(uid,{admin:true})
这似乎很管用
对象作为参数接收的方式是否有差异?您没有正确使用callable type函数。从中可以看到,传递给SDK的函数始终会接收两个参数,
数据
和上下文
,无论从应用程序传递什么。从应用程序传递的单个对象将成为单个数据
参数。不能传递多个参数,并且该参数不会分解为多个参数
您应该做的是将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将帮助您调试函数正在执行的操作。如果您记录了
uid
和claims
的值,这可能会更容易理解。非常感谢!现在这很有道理:)