skip to Main Content

i am getting an error like below and i tried to google it but i can’t fix it

i am using bcryptjs library to encrypt password but it is not able to encrypt and send to sever

Error type

error Error: Illegal arguments: undefined, number
at _async (webpack-internal:///(rsc)/./node_modules/bcryptjs/dist/bcrypt.js:170:47)
at eval (webpack-internal:///(rsc)/./node_modules/bcryptjs/dist/bcrypt.js:176:13)
at new Promise ()
at bcrypt.hash (webpack-internal:///(rsc)/./node_modules/bcryptjs/dist/bcrypt.js:175:23)
at POST (webpack-internal:///(rsc)/./src/app/api/auth/register/route.ts:17:82)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:253:37)

my code

route.ts

import User from '../../../../../models/User';
import connect from '../../../../../server/database';
import bcrypt from 'bcryptjs';
import { NextResponse } from 'next/server';

export const POST = async (request) => {
  const { name, email, password } = await request.json();

  await connect();

  const hashedPassword = await bcrypt.hash(password, 5);

  const newUser = new User({
    name,
    email,
    password: hashedPassword,
  });

  try {
    await newUser.save();
    return new NextResponse('User has been created', {
      status: 201,
    });
  } catch (err) {
    return new NextResponse(err.message, {
      status: 500,
    });
  }
};

user model

import mongoose from 'mongoose';
import postsSchema from './Posts';
import commentSchema from './Comment';

const userSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      require: true,
    },
    userName: {
      type: String,
    },
    email: {
      type: String,
      unique: true,
      require: true,
    },
    password: {
      type: String,
      require: true,
    },
    registrationDate: {
      type: Date,
      default: Date.now,
    },
    avatar: {
      type: String,
    },
    admin: {
      type: Boolean,
      default: false,
      require: true,
    },
    birthDate: {
      type: Date,
    },
    posts: [postsSchema],
    comments: [commentSchema],
  },
  { timestamps: true },
);

export default mongoose.models.User || mongoose.model('User', userSchema);

package

{
  "name": "khuongviettai",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@ckeditor/ckeditor5-build-classic": "^39.0.0",
    "@ckeditor/ckeditor5-react": "^6.1.0",
    "@types/node": "20.4.5",
    "@types/react": "18.2.18",
    "@types/react-dom": "18.2.7",
    "axios": "^1.4.0",
    "eslint": "8.46.0",
    "eslint-config-next": "13.4.12",
    "formik": "^2.4.3",
    "next": "13.4.12",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "swiper": "^10.1.0",
    "typescript": "5.1.6",
    "yup": "^1.2.0"
  },
  "devDependencies": {
    "@types/bcryptjs": "^2.4.2",
    "@types/react-google-recaptcha": "^2.1.5",
    "bcryptjs": "^2.4.3",
    "mongoose": "^7.4.2",
    "next-auth": "^4.22.3",
    "prettier": "^3.0.1",
    "sass": "^1.64.2"
  }
}

2

Answers


  1. In route.ts, change the following:

    from

    const hashedPassword = await bcrypt.hash(password, 5);
    

    to

    const hashedPassword = await bcrypt.hash(password, parseInt(5, 10));
    
    Login or Signup to reply.
  2. "Illegal arguments: undefined, number". Bcrypt is telling you password, 5 is received as undefined, number. This means password is undefined. Do a console.log(password) right after const { name, email, password } = await request.json(); and it should say undefined. If that’s the case then there’s an issue with how you send or catch the password variable.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search