获取 bcrypt 所需的错误数据和盐参数?

Getting error data and salt arguments required on bcrypt?

我正在尝试将用户保存到 MongoDB,如下所示,但出现错误 bcrypt 错误:需要数据和散列参数。我已经检查了 Whosebug 上其他开发人员提出的相同错误问题,但没有帮助。我已经附上了模型文件和路由器文件的代码。

  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;

您可以在 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