簡體   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