简体   繁体   English

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

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

I'm fairly new to nodejs and I'm struggling with this bcrypt error right now.我是 nodejs 的新手,我现在正在为这个 bcrypt 错误而苦苦挣扎。 When I use postman to send the post request(raw-data) it works without any errors, but when I send the request through form-data or from frontend, it will throw an error:当我使用 postman 发送 post 请求(原始数据)时,它没有任何错误,但是当我通过表单数据或从前端发送请求时,它会抛出错误:

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

here is what my code looks like:这是我的代码的样子:

//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
}

in my frontend:在我的前端:

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

How can I fix this error?我该如何解决这个错误?

ps: about the form-data error I tried to use body-parser as the middleware but the output is still the same. ps:关于表单数据错误,我尝试使用body-parser作为中间件,但输出仍然相同。

The body parser middle-ware has to be configured like so:主体解析器中间件必须像这样配置:

app.use(bodyParser.urlencoded());

if you are using the latest version of express:如果您使用的是最新版本的 express:

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

This should remove the error that is caused because the server is not able to parse form data.这应该消除由于服务器无法解析表单数据而导致的错误。

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

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