简体   繁体   中英

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. 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:

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.

The body parser middle-ware has to be configured like so:

app.use(bodyParser.urlencoded());

if you are using the latest version of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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