执行signUp函数时出现未定义的情况。

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

Getting undefined when executing signUp function

问题

以下是您要翻译的代码部分:

  1. async function signUp(info) {
  2. const {
  3. firstName,
  4. lastName,
  5. address,
  6. email,
  7. phoneNumber,
  8. password,
  9. city,
  10. postal_code,
  11. } = info;
  12. console.log("only info: ", phoneNumber);
  13. const queryInsertNewUser = `INSERT INTO public."Users"(
  14. "First_Name", "Email", "Mobile", "Address", "User_Type", "Last_Name", "password", "city","postal_code")
  15. VALUES ('${firstName}', '${email}', '${phoneNumber}', '${address}', 'Customer', '${lastName}', '${password}','${city}','${postal_code}')
  16. RETURNING user_id;`;
  17. client.query(queryInsertNewUser, (err, result) => {
  18. if (!err) {
  19. if (result.rowCount == 1) {
  20. console.log("User registered.");
  21. return {
  22. status: "Success",
  23. msg: "User Registered Successfully",
  24. user_id: result.rows[0].user_id,
  25. };
  26. } else {
  27. console.log("Not Registered.");
  28. return {
  29. status: "Error",
  30. msg: "Could not register user. Call Developer.",
  31. };
  32. }
  33. } else {
  34. console.log(err);
  35. }
  36. });
  37. }
  38. app.post("/signup", async(req, res) => {
  39. const { email } = req.body;
  40. const data = await signUp(req.body);
  41. console.log(data);
  42. });

请注意,由于JavaScript的异步性质,client.query的回调函数中的return语句不会直接返回值给signUp函数的调用者。您可能需要重构代码以处理回调函数的方式来处理异步操作,或者考虑使用await来等待client.query的结果。这是解决问题的一种方式。

英文:

I am trying to return a particular response when a data is inserted in postgresql. The query works perfectly, Inserts data into DB but before returning a response it prints undefined. Kindly check the code below.

  1. async function signUp(info) {
  2. const {
  3. firstName,
  4. lastName,
  5. address,
  6. email,
  7. phoneNumber,
  8. password,
  9. city,
  10. postal_code,
  11. } = info;
  12. console.log("only info: ", phoneNumber);
  13. const queryInsertNewUser = `INSERT INTO public."Users"(
  14. "First_Name", "Email", "Mobile", "Address", "User_Type", "Last_Name", "password", "city","postal_code")
  15. VALUES ('${firstName}', '${email}', '${phoneNumber}', '${address}', 'Customer', '${lastName}', '${password}','${city}','${postal_code}')
  16. RETURNING user_id;`;
  17. client.query(queryInsertNewUser, (err, result) => {
  18. if (!err) {
  19. if (result.rowCount == 1) {
  20. console.log("User registered.");
  21. return {
  22. status: "Success",
  23. msg: "User Registered Successfully",
  24. user_id: result.rows[0].user_id,
  25. };
  26. } else {
  27. console.log("Not Registered.");
  28. return {
  29. status: "Error",
  30. msg: "Could not register user. Call Developer.",
  31. };
  32. }
  33. } else {
  34. console.log(err);
  35. }
  36. });
  37. }
  38. app.post("/signup", async(req, res) => {
  39. const { email } = req.body;
  40. const data = await signUp(req.body);
  41. console.log(data);
  42. });

I tried shortening the code, tried async/await still does not work. If I remove the client.query part of signUp method and replace it with return " hello". the Console.log prints hello.

答案1

得分: 0

尝试以下代码,看看是否有帮助。

  1. async function signUp(info) {
  2. try {
  3. const {
  4. firstName,
  5. lastName,
  6. address,
  7. email,
  8. phoneNumber,
  9. password,
  10. city,
  11. postal_code,
  12. } = info;
  13. console.log("only info: ", phoneNumber);
  14. const queryInsertNewUser = `INSERT INTO public.Users(
  15. First_Name, Email, Mobile, Address, User_Type, Last_Name, password, city,postal_code)
  16. VALUES ($1, $2, $3, $4, 'Customer', $5, $6, $7, $8)
  17. RETURNING user_id;`;
  18. const params = [firstName, email, phoneNumber, address, lastName, password, city, postal_code]
  19. const result = await client.query(queryInsertNewUser, params);
  20. console.log("Result here", result)
  21. if (result) { // OR (result && result.rowCount)
  22. return {
  23. status: "Success",
  24. msg: "User Registered Successfully",
  25. user_id: result.rows[0].user_id,
  26. }
  27. }
  28. console.log("Not Registered.");
  29. return {
  30. status: "Error",
  31. msg: "Could not register user. Call Developer.",
  32. }
  33. } catch (error) {
  34. console.error("Error", error)
  35. return {
  36. status: false
  37. }
  38. }
  39. }

如果要使用回调进行查询,尝试将其更改如下,但我建议将其改为Promise。

  1. client.query(queryInsertNewUser, (err, result) => {
  2. if (err) {
  3. console.log(err);
  4. return {
  5. status: false
  6. }
  7. }
  8. if (result && result.rowCount) {
  9. console.log("User registered.");
  10. return {
  11. status: "Success",
  12. msg: "User Registered Successfully",
  13. user_id: result.rows[0].user_id,
  14. };
  15. } else {
  16. console.log("Not Registered.");
  17. return {
  18. status: "Error",
  19. msg: "Could not register user. Call Developer.",
  20. };
  21. }
  22. });
英文:

Try below code, see if it helps.

  1. async function signUp(info) {
  2. try {
  3. const {
  4. firstName,
  5. lastName,
  6. address,
  7. email,
  8. phoneNumber,
  9. password,
  10. city,
  11. postal_code,
  12. } = info;
  13. console.log("only info: ", phoneNumber);
  14. const queryInsertNewUser = `INSERT INTO public.Users(
  15. First_Name, Email, Mobile, Address, User_Type, Last_Name, password, city,postal_code)
  16. VALUES ($1, $2, $3, $4, 'Customer', $5, $6, $7, $8)
  17. RETURNING user_id;`;
  18. const params = [firstName, email, phoneNumber, address, lastName, password, city, postal_code]
  19. const result = await client.query(queryInsertNewUser, params);
  20. console.log("Result here", result)
  21. if (result) { // OR (result && result.rowCount)
  22. return {
  23. status: "Success",
  24. msg: "User Registered Successfully",
  25. user_id: result.rows[0].user_id,
  26. }
  27. }
  28. console.log("Not Registered.");
  29. return {
  30. status: "Error",
  31. msg: "Could not register user. Call Developer.",
  32. }
  33. } catch (error) {
  34. console.error("Error", error)
  35. return {
  36. status: false
  37. }
  38. }
  39. }

If you want to use callback for query then try changing it like below, but I'll suggest to move it to Promise

  1. client.query(queryInsertNewUser, (err, result) => {
  2. if (err) {
  3. console.log(err);
  4. return {
  5. status: false
  6. }
  7. }
  8. if (result && result.rowCount) {
  9. console.log("User registered.");
  10. return {
  11. status: "Success",
  12. msg: "User Registered Successfully",
  13. user_id: result.rows[0].user_id,
  14. };
  15. } else {
  16. console.log("Not Registered.");
  17. return {
  18. status: "Error",
  19. msg: "Could not register user. Call Developer.",
  20. };
  21. }
  22. });

huangapple
  • 本文由 发表于 2023年1月11日 03:09:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75074734.html
匿名

发表评论

匿名网友

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

确定