简体   繁体   English

非法 arguments:未定义,字符串

[英]Illegal arguments: undefined, string

I get this error when registering a user:注册用户时出现此错误:

(node:13225) UnhandledPromiseRejectionWarning: Error: Illegal arguments: undefined, string at Object.bcrypt.hashSync (/home/admin/Desktop/project/node_modules/bcryptjs/dist/bcrypt.js:189:19) at module.exports.register (/home/admin/Desktop/project/controllers/auth.js:26:30) (node:13225) UnhandledPromiseRejectionWarning: Unhandled promise rejection. (node:13225) UnhandledPromiseRejectionWarning: Error: Illegal arguments: undefined, string at Object.bcrypt.hashSync (/home/admin/Desktop/project/node_jsports/bcryptjs/dist/b.exports:18js19) atcrypt module.注册 (/home/admin/Desktop/project/controllers/auth.js:26:30) (node:13225) UnhandledPromiseRejectionWarning: 未处理的 promise 拒绝。 This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with.catch().(rejection id: 1)此错误源于在没有 catch 块的情况下抛出异步 function 内部,或拒绝未使用.catch() 处理的 promise。(拒绝 id:1)

controller: controller:

const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const db = require('../config/db.config.js')
const User = db.user
const errorHandler = require('../utils/errorHandler')

module.exports.register = async function(req, res) {
    const candidate = await User.findOne({
        where: {
            username: req.body.username
        }
    })

    if (candidate) {
        res.status(409).json({
            message: 'This login is already taken. Try another.'
        })
    } else {
        const salt = bcrypt.genSaltSync(10)
        const password = req.body.password
        const user = new User({
            name: req.body.name,
            username: req.body.username,
            roles: req.body.roles,
            photoSrc: req.file ? req.file.path: '',
            password: bcrypt.hashSync(password, salt)
        })
        try {
            await user.save()
            res.status(201).json(user)
        } catch(e) {
            errorHandler(res, e)
        }
    }
}

models:楷模:

module.exports = (sequelize, Sequelize) => {
    const User = sequelize.define('users', {
        name: {
            type: Sequelize.STRING(100),
            required: true
        },
        username: {
            type: Sequelize.STRING(40),
            required: true,
            unique: true
        },
        roles: {
            type: Sequelize.STRING(100),
            required: true
        },
        password: {
            type: Sequelize.STRING,
            required: true
        },
        photoSrc: {
            type: Sequelize.STRING(200),
            default: ''
        }
    });

    return User;
}

You need to apply await to your salt and password assignments too.您也需要将await应用于saltpassword分配。

Like this,像这样,

const salt = await bcrypt.genSaltSync(10);
const password = await req.body.password;

Hope this helps!.希望这可以帮助!。

For every async operation, we have to await对于每个异步操作,我们必须等待

const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(req.body.password, salt);

working for me like this way, instead of cb, you can use async-await像这样为我工作,而不是 cb,你可以使用 async-await

bcrypt.genSalt(10, function(err, salt) {
  bcrypt.hash("password", salt, function(err, hash) {
    // Store hash in your password DB.
  });
});

Thanks!!谢谢!!

I encountered the same error using lambda function, but the problem with me was parsing the req body so i had to我在使用 lambda 函数时遇到了同样的错误,但我的问题是解析 req 主体,所以我不得不

const body = JSON.parse(event.body) // in case of lambda function

hope this helps someone.希望这有助于某人。

This happened with me when I was verifying my registration form, I was searching for the fix but didn't get a proper solution.当我验证我的注册表时发生了这种情况,我正在寻找修复程序但没有得到正确的解决方案。

How I fixed it Later I realised that I have not passed the required fields before sending post request.我是如何修复的后来我意识到在发送 post 请求之前我没有通过必填字段。 Before Sending Post request through Postman make sure You have passed key and value in postman在通过Postman发送Post 请求之前,确保您已经在 postman 中传递了key 和 value

Click on Above link to see example.单击上面的链接查看示例。

I was missing the "OTP" field in Schema我错过了架构中的“OTP”字段

Old:老的:

  Schema(
{
  email: {
    type: String,
    requird: true,
  },
  createdAt: {
    type: Date,
    default: Date.now,
    index: { expires: 300 },
    // after 5 mins it get's deleted
  },
},
{ timestamps: true }

) )

New:新的:

  Schema(
{
  email: {
    type: String,
    requird: true,
  },
  otp: {
    type: String,
    required: true,
  },
  createdAt: {
    type: Date,
    default: Date.now,
    index: { expires: 300 },
    // after 5 mins it get's deleted
  },
},
{ timestamps: true }

) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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