NodeJS 注册时检索信息

pxq42qpu  于 2022-12-18  发布在  Node.js
关注(0)|答案(1)|浏览(107)

我该怎么做呢?当用户注册时,我希望端点仍然继续并取回保存在数据库中的信息。由于某种原因,它没有按预期工作我该怎么做:
我的代码如下所示:

app.post("/api/sign-up", async function (req, res) {
  dbConn.query(
    `select * from accounts where email = ${dbConn.escape(req.body.email)}`,
    async function (err, result, fields) {
      if (result.length === 0) {
        var email = req.body.email;
        var phone = req.body.phone;
        var password = req.body.password;
        var fullname = "NULL";

        const hashPass = await bcrypt.hash(password, 12);

        dbConn.query(
          `insert into accounts(email, phone, password, fullname) values (?,?,?,?)`,
          [email, phone, hashPass, fullname],
          function (error, results, fields) {
            if (error) throw error;

            return res.send({
              error: false,
              data: results[0],
              message: "User created Successfully",
            });
          }
        );
      } else {
        return res.send({
          error: true,
          message: "User exists",
        });
      }
    }
  );
});

我在网上查了一下,找不到需要的信息。

gfttwv5a

gfttwv5a1#

我想办法修好了。
代码现在如下所示,它显示了POST man内部的数据

app.post("/api/sign-up", async function (req, res) {
  dbConn.query(
    `select * from accounts where email = ${dbConn.escape(req.body.email)}`,
    async function (err, result, fields) {
      if (result.length === 0) {
        var email = req.body.email;
        var phone = req.body.phone;
        var password = req.body.password;
        var fullname = "NULL";

        const hashPass = await bcrypt.hash(password, 12);

        dbConn.query(
          `insert into accounts(email, phone, password, fullname) values (?,?,?,?)`,
          [email, phone, hashPass, fullname],
          function (error, results, fields) {
            if (error) throw error;

            return res.send({
              error: false,
              email:email,phone:phone,
              message: "User created Successfully",
            });
            

            //return res.status(201).json({message: 'User created Successfully', "email":email,"phone":phone});
          }
        );
      } else {
        return res.send({
          error: true,
          message: "User exists",
        });
      }
    }
  );
});

感谢每一个决定看一看的人:)

相关问题