无法从我的 Firebase 可调用函数中提取参数。

huangapple go评论110阅读模式
英文:

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

  1. const createUserWithRole = httpsCallable(functions, "createUserAndAddUserRole");
  2. console.log(form) // WORKS
  3. const response = await createUserWithRole({
  4. email: form.email,
  5. password: form.password,
  6. role: form.role,
  7. });

Functions.js

  1. const admin = require("firebase-admin");
  2. // const cors = require("cors")({origin: true}); // Add cors middleware
  3. const {onCall} = require("firebase-functions/v2/https");
  4. const logger = require("firebase-functions/logger");
  5. admin.initializeApp();
  6. exports.createUserAndAddUserRole = onCall(async (data, context) => {
  7. // logger 1
  8. logger.log("data", data);
  9. // const email = "bright@gmail.com"
  10. // const password = "bright"
  11. // const role = "admin"
  12. const userRecord = await admin
  13. .auth()
  14. .createUser({
  15. email,
  16. password,
  17. });
  18. logger.log("User Record", userRecord);
  19. return admin.auth().getUserByEmail(data.email).then((user)=> {
  20. return admin.auth().setCustomUserClaims(user.uid, {
  21. role: role,
  22. });
  23. }).then(() => {
  24. console.log("Claims Set");
  25. const userDataAfter = admin.getUserByEmail(data.email);
  26. return {
  27. status: 200,
  28. message: `Success! ${data.email} has been made ${data.role} role`,
  29. };
  30. }).catch((err) => {
  31. return {
  32. status: 500,
  33. message: err,
  34. };
  35. });
  36. });

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

  1. const createUserWithRole = httpsCallable(functions, "createUserAndAddUserRole");
  2. console.log(form) // WORKS
  3. const response = await createUserWithRole({
  4. email: form.email,
  5. password: form.password,
  6. role: form.role,
  7. });

Functions.js

  1. const admin = require("firebase-admin");
  2. // const cors = require("cors")({origin: true}); // Add cors middleware
  3. const {onCall} = require("firebase-functions/v2/https");
  4. const logger = require("firebase-functions/logger");
  5. admin.initializeApp();
  6. exports.createUserAndAddUserRole = onCall(async (data, context) => {
  7. // logger 1
  8. logger.log("data", data);
  9. // const email = "bright@gmail.com";
  10. // const password = "bright";
  11. // const role = "admin";
  12. const userRecord = await admin
  13. .auth()
  14. .createUser({
  15. email,
  16. password,
  17. });
  18. logger.log("User Record", userRecord);
  19. return admin.auth().getUserByEmail(data.email).then((user)=> {
  20. return admin.auth().setCustomUserClaims(user.uid, {
  21. role: role,
  22. });
  23. }).then(() => {
  24. console.log("Claims Set");
  25. const userDataAfter = admin.getUserByEmail(data.email);
  26. return {
  27. status: 200,
  28. message: `Success! ${data.email} has been made ${data.role} role`,
  29. };
  30. }).catch((err) => {
  31. return {
  32. status: 500,
  33. message: err,
  34. };
  35. });
  36. });

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) => {...}); (第二代)。

因此,你需要按照以下方式操作:

  1. exports.createUserAndAddUserRole = onCall(async (request) => {
  2. const email = request.data.email;
  3. const password = request.data.password;
  4. const role = request.data.role;
  5. // ...
英文:

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:

  1. exports.createUserAndAddUserRole = onCall(async (request) => {
  2. const email = request.data.email;
  3. const password = request.data.password;
  4. const role = request.data.role;
  5. // ...

huangapple
  • 本文由 发表于 2023年6月29日 19:08:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定