首页 > 解决方案 > 获取 bcrypt 所需的错误数据和盐参数?

问题描述

我正在尝试按如下方式将用户保存到 MongoDB,但我收到错误 bcrypt Error: data and hash arguments required。我检查了 StackOverflow 上其他开发人员提出的相同错误问题,但没有帮助。我附上了模型文件和路由器文件的代码。

  1. 用户模型文件
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');

const uSchema = new mongoose.Schema({
    fullName: {
        type: String,
        required: true,
        min: 4,
        max: 30
    },
    email: {
        type: String,
        required: true,
        trim: true,
        unique: true,
        index: true
    },
    hash_password: {
        type: String,
        required: true,
        min: 6,
        max: 12
    },
    role: {
        type: String,
        enum: ['user', 'admin', 'moderator'],
        default: 'admin'
    }
}, { timestamps: true });

uSchema.virtual('password')
    .set(function (password) {
        this.hash_password = bcrypt.hashSync(password, 10);
    });

uSchema.methods = {
    authenticate: function (password) {
        return bcrypt.compareSync(password, this.hash_password);
    }
}


module.exports = mongoose.model('User', uSchema);

  1. 用户路由器文件
const express = require('express');
const router = express.Router();
const User = require('../models/user.model');

router.post('/login', (req, res) => {

});

router.post('/signin', (req, res) => {
    User.findOne({ email: req.body.email })
        .exec((error, user) => {
            if (user) return res.status(400).json({
                message: 'User already exists.'
            });

            const {
                fullName,
                email,
                password
            } = req.body;

            const _user = new User({
                fullName,
                email,
                password
            });

            _user.save((error, data) => {
                if (error) {
                    return res.status(400).json({
                        message: 'Something went wrong'
                    });
                } if (data) {
                    return res.status(201).json({
                        user: data
                    })
                }
            })
        });
});

module.exports = router;

标签: node.jsmongodbbcrypt

解决方案


您可以router改为在文件中执行此操作。

const bcrypt = require("bcrypt")

// ...

router.post('/signin', (req, res) => { // Change this to signup
    User.findOne({ email: req.body.email })
        .exec((error, user) => {
            if (user) return res.status(400).json({
                message: 'User already exists.'
            });

            const {
                fullName,
                email,
                password
            } = req.body;

            const hashedPassword = bcrypt.hashSync(password, 10);

            const _user = new User({
                fullName,
                email,
                hashedPassword
            });

            _user.save((error, data) => {
                if (error) {
                    return res.status(400).json({
                        message: 'Something went wrong'
                    });
                } if (data) {
                    return res.status(201).json({
                        user: data
                    })
                }
            })
        });
});

module.exports = router;

virtual并从模型中删除密码。


推荐阅读