You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js中前端发送POST请求时Bcrypt报Illegal arguments错误的解决求助

Node.js中前端发送POST请求时Bcrypt报Illegal arguments错误的解决求助

大家好,我是刚接触Node.js的新手,现在遇到一个Bcrypt相关的问题卡壳了,想请各位帮忙看看。

情况是这样的:我用Postman发送raw JSON格式的POST请求时,后端完全正常,没有任何错误;但如果用Postman的form-data格式,或者直接通过前端表单发送请求,后端就会抛出这个错误:

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

下面是我的代码片段:

后端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);
  } catch(err){
    // 这里是一些错误处理逻辑
  }
}

前端表单代码

<form action="http://localhost:3000/sign-up" method="post">
  <!-- 这里是用户名、邮箱、密码的输入框 -->
</form>

我试过用body-parser中间件来处理form-data,但结果还是一样的错误,实在不知道哪里出问题了,有没有大佬能指点一下怎么解决这个问题?

备注:内容来源于stack exchange,提问作者kevin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.23 07:52:57