bcryptjs 非法参数: 未定义, 数字

huangapple go评论86阅读模式
英文:

bcryptjs Illegal arguments: undefined, number

问题

我遇到了以下错误,并尝试在谷歌上搜索,但无法修复:

我正在使用bcryptjs库来加密密码,但它无法加密并发送到服务器。

错误类型

错误 Error: 非法参数:未定义,数字
在 _async (webpack-internal:///(rsc)/./node_modules/bcryptjs/dist/bcrypt.js:170:47)
在 eval (webpack-internal:///(rsc)/./node_modules/bcryptjs/dist/bcrypt.js:176:13)
在 new Promise ()
在 bcrypt.hash (webpack-internal:///(rsc)/./node_modules/bcryptjs/dist/bcrypt.js:175:23)
在 POST (webpack-internal:///(rsc)/./src/app/api/auth/register/route.ts:17:82)
在 process.processTicksAndRejections (node:internal/process/task_queues:95:5)
在 async eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:253:37)

我的代码:

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('用户已创建', {
      status: 201,
    });
  } catch (err) {
    return new NextResponse(err.message, {
      status: 500,
    });
  }
};

用户模型

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);

{
  "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"
  }
}
英文:

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 (<anonymous>)
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 &#39;../../../../../models/User&#39;;
import connect from &#39;../../../../../server/database&#39;;
import bcrypt from &#39;bcryptjs&#39;;
import { NextResponse } from &#39;next/server&#39;;

export const POST = async (request) =&gt; {
  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(&#39;User has been created&#39;, {
      status: 201,
    });
  } catch (err) {
    return new NextResponse(err.message, {
      status: 500,
    });
  }
};

user model

import mongoose from &#39;mongoose&#39;;
import postsSchema from &#39;./Posts&#39;;
import commentSchema from &#39;./Comment&#39;;

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(&#39;User&#39;, userSchema);

package

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

答案1

得分: 2

"非法参数: 未定义, 数字"。Bcrypt 告诉您 password, 5 被解释为 未定义, 数字。这意味着密码是未定义的。在 const { name, email, password } = await request.json(); 之后立即执行 console.log(password),它应该显示为 undefined。如果是这种情况,那么发送或捕获密码变量的方式可能有问题。

英文:

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

答案2

得分: 0

route.ts 中,将以下内容更改为:

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

const hashedPassword = await bcrypt.hash(password, parseInt(5, 10));
英文:

In route.ts, change the following:

from

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

to

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

huangapple
  • 本文由 发表于 2023年8月9日 15:29:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865515-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定