bcryptjs非法参数:undefined,数字

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

bcryptjs Illegal arguments: undefined, number

问题

我遇到了以下错误,并尝试通过谷歌搜索来解决,但是没有成功:

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

错误类型:

错误 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)

我的代码如下:

route.ts

  1. import User from '../../../../../models/User';
  2. import connect from '../../../../../server/database';
  3. import bcrypt from 'bcryptjs';
  4. import { NextResponse } from 'next/server';
  5. export const POST = async (request) => {
  6. const { name, email, password } = await request.json();
  7. await connect();
  8. const hashedPassword = await bcrypt.hash(password, 5);
  9. const newUser = new User({
  10. name,
  11. email,
  12. password: hashedPassword,
  13. });
  14. try {
  15. await newUser.save();
  16. return new NextResponse('User has been created', {
  17. status: 201,
  18. });
  19. } catch (err) {
  20. return new NextResponse(err.message, {
  21. status: 500,
  22. });
  23. }
  24. };

user model

  1. import mongoose from 'mongoose';
  2. import postsSchema from './Posts';
  3. import commentSchema from './Comment';
  4. const userSchema = new mongoose.Schema(
  5. {
  6. name: {
  7. type: String,
  8. require: true,
  9. },
  10. userName: {
  11. type: String,
  12. },
  13. email: {
  14. type: String,
  15. unique: true,
  16. require: true,
  17. },
  18. password: {
  19. type: String,
  20. require: true,
  21. },
  22. registrationDate: {
  23. type: Date,
  24. default: Date.now,
  25. },
  26. avatar: {
  27. type: String,
  28. },
  29. admin: {
  30. type: Boolean,
  31. default: false,
  32. require: true,
  33. },
  34. birthDate: {
  35. type: Date,
  36. },
  37. posts: [postsSchema],
  38. comments: [commentSchema],
  39. },
  40. { timestamps: true },
  41. );
  42. export default mongoose.models.User || mongoose.model('User', userSchema);

package.json

  1. {
  2. "name": "khuongviettai",
  3. "version": "0.1.0",
  4. "private": true,
  5. "scripts": {
  6. "dev": "next dev",
  7. "build": "next build",
  8. "start": "next start",
  9. "lint": "next lint"
  10. },
  11. "dependencies": {
  12. "@ckeditor/ckeditor5-build-classic": "^39.0.0",
  13. "@ckeditor/ckeditor5-react": "^6.1.0",
  14. "@types/node": "20.4.5",
  15. "@types/react": "18.2.18",
  16. "@types/react-dom": "18.2.7",
  17. "axios": "^1.4.0",
  18. "eslint": "8.46.0",
  19. "eslint-config-next": "13.4.12",
  20. "formik": "^2.4.3",
  21. "next": "13.4.12",
  22. "react": "18.2.0",
  23. "react-dom": "18.2.0",
  24. "swiper": "^10.1.0",
  25. "typescript": "5.1.6",
  26. "yup": "^1.2.0"
  27. },
  28. "devDependencies": {
  29. "@types/bcryptjs": "^2.4.2",
  30. "@types/react-google-recaptcha": "^2.1.5",
  31. "bcryptjs": "^2.4.3",
  32. "mongoose": "^7.4.2",
  33. "next-auth": "^4.22.3",
  34. "prettier": "^3.0.1",
  35. "sass": "^1.64.2"
  36. }
  37. }
英文:

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

  1. import User from &#39;../../../../../models/User&#39;;
  2. import connect from &#39;../../../../../server/database&#39;;
  3. import bcrypt from &#39;bcryptjs&#39;;
  4. import { NextResponse } from &#39;next/server&#39;;
  5. export const POST = async (request) =&gt; {
  6. const { name, email, password } = await request.json();
  7. await connect();
  8. const hashedPassword = await bcrypt.hash(password, 5);
  9. const newUser = new User({
  10. name,
  11. email,
  12. password: hashedPassword,
  13. });
  14. try {
  15. await newUser.save();
  16. return new NextResponse(&#39;User has been created&#39;, {
  17. status: 201,
  18. });
  19. } catch (err) {
  20. return new NextResponse(err.message, {
  21. status: 500,
  22. });
  23. }
  24. };

user model

  1. import mongoose from &#39;mongoose&#39;;
  2. import postsSchema from &#39;./Posts&#39;;
  3. import commentSchema from &#39;./Comment&#39;;
  4. const userSchema = new mongoose.Schema(
  5. {
  6. name: {
  7. type: String,
  8. require: true,
  9. },
  10. userName: {
  11. type: String,
  12. },
  13. email: {
  14. type: String,
  15. unique: true,
  16. require: true,
  17. },
  18. password: {
  19. type: String,
  20. require: true,
  21. },
  22. registrationDate: {
  23. type: Date,
  24. default: Date.now,
  25. },
  26. avatar: {
  27. type: String,
  28. },
  29. admin: {
  30. type: Boolean,
  31. default: false,
  32. require: true,
  33. },
  34. birthDate: {
  35. type: Date,
  36. },
  37. posts: [postsSchema],
  38. comments: [commentSchema],
  39. },
  40. { timestamps: true },
  41. );
  42. export default mongoose.models.User || mongoose.model(&#39;User&#39;, userSchema);

package

  1. {
  2. &quot;name&quot;: &quot;khuongviettai&quot;,
  3. &quot;version&quot;: &quot;0.1.0&quot;,
  4. &quot;private&quot;: true,
  5. &quot;scripts&quot;: {
  6. &quot;dev&quot;: &quot;next dev&quot;,
  7. &quot;build&quot;: &quot;next build&quot;,
  8. &quot;start&quot;: &quot;next start&quot;,
  9. &quot;lint&quot;: &quot;next lint&quot;
  10. },
  11. &quot;dependencies&quot;: {
  12. &quot;@ckeditor/ckeditor5-build-classic&quot;: &quot;^39.0.0&quot;,
  13. &quot;@ckeditor/ckeditor5-react&quot;: &quot;^6.1.0&quot;,
  14. &quot;@types/node&quot;: &quot;20.4.5&quot;,
  15. &quot;@types/react&quot;: &quot;18.2.18&quot;,
  16. &quot;@types/react-dom&quot;: &quot;18.2.7&quot;,
  17. &quot;axios&quot;: &quot;^1.4.0&quot;,
  18. &quot;eslint&quot;: &quot;8.46.0&quot;,
  19. &quot;eslint-config-next&quot;: &quot;13.4.12&quot;,
  20. &quot;formik&quot;: &quot;^2.4.3&quot;,
  21. &quot;next&quot;: &quot;13.4.12&quot;,
  22. &quot;react&quot;: &quot;18.2.0&quot;,
  23. &quot;react-dom&quot;: &quot;18.2.0&quot;,
  24. &quot;swiper&quot;: &quot;^10.1.0&quot;,
  25. &quot;typescript&quot;: &quot;5.1.6&quot;,
  26. &quot;yup&quot;: &quot;^1.2.0&quot;
  27. },
  28. &quot;devDependencies&quot;: {
  29. &quot;@types/bcryptjs&quot;: &quot;^2.4.2&quot;,
  30. &quot;@types/react-google-recaptcha&quot;: &quot;^2.1.5&quot;,
  31. &quot;bcryptjs&quot;: &quot;^2.4.3&quot;,
  32. &quot;mongoose&quot;: &quot;^7.4.2&quot;,
  33. &quot;next-auth&quot;: &quot;^4.22.3&quot;,
  34. &quot;prettier&quot;: &quot;^3.0.1&quot;,
  35. &quot;sass&quot;: &quot;^1.64.2&quot;
  36. }
  37. }

答案1

得分: 2

"非法参数:undefined,number"。Bcrypt 告诉你 password, 5 被解析为 undefined, number。这意味着密码是未定义的。在 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中,将以下内容更改为:

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

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

In route.ts, change the following:

from

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

to

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

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

发表评论

匿名网友

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

确定