繁体   English   中英

Bcrypt 错误:非法参数:未定义,通过前端发送发布请求时的数字

[英]Bcrypt Error: Illegal arguments: undefined, number when sending post request through frontend

我是 nodejs 的新手,我现在正在为这个 bcrypt 错误而苦苦挣扎。 当我使用 postman 发送 post 请求(原始数据)时,它没有任何错误,但是当我通过表单数据或从前端发送请求时,它会抛出错误:

nextTick(callback.bind(this, Error("Illegal arguments: "+(typeof s)+', '+(typeof salt))));
                                             ^
Error: Illegal arguments: undefined, number

这是我的代码的样子:

//backend authcontroller

export const signUpLogic = async (req: Request, res: Response) => {
  const { username, email, password } = req.body;
  try {
    const salt = await bcrypt.genSalt(10);
    const hashedPassword = await bcrypt.hash(password, salt);
    //
    const result = userSchema.safeParse({
      username,
      email,
      password: String(hashedPassword),
    });
    //
    res.status(200).json(result);
  }cath(err){
//some error handling
}

在我的前端:

    <form action="http://localhost:3000/sign-up" method="post">
    <!-- some input fields here-->
    </form>

我该如何解决这个错误?

ps:关于表单数据错误,我尝试使用body-parser作为中间件,但输出仍然相同。

主体解析器中间件必须像这样配置:

app.use(bodyParser.urlencoded());

如果您使用的是最新版本的 express:

app.use(express.urlencoded({
    extended: true
}))

这应该消除由于服务器无法解析表单数据而导致的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM