首页 > 解决方案 > 快速路由错误“Err: TypeError: Cannot read property 'type' of undefined”

问题描述

我有注册用户的基本路线,它运行一个存储过程检查电子邮件是否存在于我的 SQL 数据库中,然后运行一个存储过程更新记录(如果不存在)。

但是我不断收到错误

 Err:  TypeError: Cannot read property 'type' of undefined

这是我的路线

app.post("/register", async (req, response) => {
  try {
    await sql.connect(config);
    const Email = req.body.email;
    const PasswordHash = req.body.password;
    const UserEmail = req.body.email;

    var request = new sql.Request();

    request.input("Email", sql.VarChar, Email);
    //request.input("Password", sql.VarChar, Password);

    const result = await request.execute("dbo.CheckEmailExists");

    if (result.recordsets[0].length > 0) {
      console.info("These credentials already exist");
      console.log(req.body);
    } else {
      console.info("these credentials do not exist well done");

      console.log({ UserEmail });
      request.input("UserEmail", sql.nVarChar, UserEmail);
      request.input("PasswordHash", sql.nVarChar, PasswordHash);
      request.execute("dbo.RegisterUser");

      console.log("done done");
    }
  } catch (err) {
    console.log("Err: ", err);
    response.status(500).send("Check api console.log for the error");
  }
});

为什么会发生这种情况,我该如何解决?

ALTER PROCEDURE [dbo].[RegisterUser]
  @UserEmail nvarchar(320),
  @PasswordHash nvarchar(100)

  AS

  INSERT into RegisteredUsers (Email,PasswordHash) values (@UserEmail,  HASHBYTES('SHA2_512', @PasswordHash ))






标签: sqlnode.jssql-serverexpress

解决方案


它应该是sql.NVarChar(N 应该是大写字母)。

希望这可以帮助!


推荐阅读