英文:
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 '../../../../../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"
}
}
答案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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论