英文:
Cant extract arguments from my firebase callable function
问题
以下是您要翻译的内容:
"logging the form data i.e email, password and role, shows that correct data is being passed to the function
App.js file
const createUserWithRole = httpsCallable(functions, "createUserAndAddUserRole");
console.log(form) // WORKS
const response = await createUserWithRole({
email: form.email,
password: form.password,
role: form.role,
});
Functions.js
const admin = require("firebase-admin");
// const cors = require("cors")({origin: true}); // Add cors middleware
const {onCall} = require("firebase-functions/v2/https");
const logger = require("firebase-functions/logger");
admin.initializeApp();
exports.createUserAndAddUserRole = onCall(async (data, context) => {
// logger 1
logger.log("data", data);
// const email = "bright@gmail.com"
// const password = "bright"
// const role = "admin"
const userRecord = await admin
.auth()
.createUser({
email,
password,
});
logger.log("User Record", userRecord);
return admin.auth().getUserByEmail(data.email).then((user)=> {
return admin.auth().setCustomUserClaims(user.uid, {
role: role,
});
}).then(() => {
console.log("Claims Set");
const userDataAfter = admin.getUserByEmail(data.email);
return {
status: 200,
message: `Success! ${data.email} has been made ${data.role} role`,
};
}).catch((err) => {
return {
status: 500,
message: err,
};
});
});
running logger 1 prints out an object that seems to be a request object with the data I passed being under a subObject jsonPayload.data
The documentation for onCall directs that simply using data.email will work but that doesnt work.
Am i passing the data wrong? How do i pass the data or extract it from the jsonPayload.data object?"
英文:
logging the form data i.e email, password and role, shows that correct data is being passed to the function
App.js file
const createUserWithRole = httpsCallable(functions, "createUserAndAddUserRole");
console.log(form) // WORKS
const response = await createUserWithRole({
email: form.email,
password: form.password,
role: form.role,
});
Functions.js
const admin = require("firebase-admin");
// const cors = require("cors")({origin: true}); // Add cors middleware
const {onCall} = require("firebase-functions/v2/https");
const logger = require("firebase-functions/logger");
admin.initializeApp();
exports.createUserAndAddUserRole = onCall(async (data, context) => {
// logger 1
logger.log("data", data);
// const email = "bright@gmail.com";
// const password = "bright";
// const role = "admin";
const userRecord = await admin
.auth()
.createUser({
email,
password,
});
logger.log("User Record", userRecord);
return admin.auth().getUserByEmail(data.email).then((user)=> {
return admin.auth().setCustomUserClaims(user.uid, {
role: role,
});
}).then(() => {
console.log("Claims Set");
const userDataAfter = admin.getUserByEmail(data.email);
return {
status: 200,
message: `Success! ${data.email} has been made ${data.role} role`,
};
}).catch((err) => {
return {
status: 500,
message: err,
};
});
});
running logger 1 prints out an object that seems to be a request object with the data I passed being under a subObject jsonPayload.data
The documentation for onCall directs that simply using data.email will work but that doesnt work.
Am i passing the data wrong? How do i pass the data or extract it from the jsonPayload.data object?
答案1
得分: 2
你使用的是第二代云函数(参见 const {onCall} = require("firebase-functions/v2/https");
)。正如在可调用的第二代云函数文档中所示,传递给回调函数的参数已从第一代的
onCall((data, context) => {...});
变为
onCall((request) => {...});
(第二代)。
因此,你需要按照以下方式操作:
exports.createUserAndAddUserRole = onCall(async (request) => {
const email = request.data.email;
const password = request.data.password;
const role = request.data.role;
// ...
英文:
You use Cloud Functions 2n gen (cf. const {onCall} = require("firebase-functions/v2/https");
). As you can see in the doc for Callable Cloud Functions 2nd generation the arguments passed to the callback function have changed from
onCall((data, context) => {...});
(1st gen)
to
onCall((request) => {...});
(2nd gen)
So you need to do as follows:
exports.createUserAndAddUserRole = onCall(async (request) => {
const email = request.data.email;
const password = request.data.password;
const role = request.data.role;
// ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论