【问题标题】:Illegal arguments: undefined, string非法参数:未定义,字符串
【发布时间】:2019-03-29 16:29:44
【问题描述】:

注册用户时出现此错误:

(节点:13225)UnhandledPromiseRejectionWarning:错误:非法 参数:未定义,字符串 在 Object.bcrypt.hashSync (/home/admin/Desktop/project/node_modules/bcryptjs/dist/bcrypt.js:189:19) 在 module.exports.register (/home/admin/Desktop/project/controllers/auth.js:26:30) (node:13225) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这 错误源于在异步函数内部抛出 没有 catch 块,或拒绝未处理的承诺 with .catch().(rejection id: 1)

控制器:

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

型号:

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

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    您还需要将await 应用于您的saltpassword 分配。

    这样,

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

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      对于每个异步操作,我们都必须等待

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

      【讨论】:

        【解决方案3】:

        我在验证注册表单时发生了这种情况,我正在寻找解决方案,但没有找到合适的解决方案。

        我是如何解决的 后来我意识到在发送 post 请求之前我没有通过必填字段。 Before Sending Post request through Postman make sure You have passed key and value in postman

        点击以上链接查看示例。

        【讨论】:

        • 这对我来说是最好的答案
        【解决方案4】:

        像这样为我工作,而不是 cb,你可以使用 async-await

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

        谢谢!!

        【讨论】:

          【解决方案5】:

          我在使用 lambda 函数时遇到了同样的错误,但我的问题是解析 req 正文,所以我不得不

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

          希望这对某人有所帮助。

          【讨论】:

            猜你喜欢
            • 2021-07-21
            • 1970-01-01
            • 2021-04-20
            • 1970-01-01
            • 2022-12-17
            • 2021-05-17
            • 2018-06-22
            • 2019-06-01
            • 1970-01-01
            相关资源
            最近更新 更多