【问题标题】:pre function not called in mongoosemongoose 中未调用 pre 函数
【发布时间】:2020-01-28 19:19:02
【问题描述】:

我正在尝试使用 mongoose 和 mongodb 加密注册密码,但根本没有调用 pre 函数。

var mongoose = require('mongoose');
var Schema = mongoose.Schema,
    bcrypt = require('bcrypt'),
    SALT_WORK_FACTOR = 10;


var patientSchema = new Schema({
    username: {type: String, trim: true, index: { unique: true }},
    password: {type: String, required: true}
});

//====================== Middleware:Start==========================//
patientSchema.pre('save', function(next) {
    console.log('pre called'); //This is not printed at all
    var user = this;

    // only hash the password if it has been modified (or is new)
    if (!user.isModified('password')) return next();

    // generate a salt
    bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
        if (err) return next(err);

        // hash the password along with our new salt
        bcrypt.hash(user.password, salt, function(err, hash) {
            if (err) return next(err);

            // override the cleartext password with the hashed one
            user.password = hash;
            next();
        });
    });
});
//======================Middleware:End===========================//

//======================API Routes:Start===========================//
router.route('/signup')
        .post(function (req, res) {
            console.log('post signup called', req.body);
            var patients = new Patients({
                username: req.body.username,
                password: req.body.password
            });

            Patients.findOne({username: req.body.username}, function (err, user) {

                if (err) {
                    console.log('user not found');
                }

                if (user) {
                    console.log("patient already exists");
                    res.json({message: 'patient already exists'});
                } else {
                    //Saving the model instance to the DB
                    patients.save(function (err) {
                        if (err)
                            throw err;
                        console.log("user Saved Successfully");
                        res.json({message: 'user Saved Successfully'});
                    });
                }

            });

        });

module.exports = router;
//======================API Routes:End===========================//

pre 函数内部,根本不打印console.log('pre called');。我在这里错过了什么?

【问题讨论】:

  • 听起来您创建了 Patients 模型,然后 然后 将中间件添加到架构中。您需要在创建模型之前添加中间件
  • @robertklep 你是对的。谢谢

标签: node.js mongodb mongoose mongoose-schema


【解决方案1】:

它可能会解决你的错误。

const patients = new Patients({
    username: req.body.username,
    password: req.body.password
})

if(!patients) return res.json(patients)
patients.save((err,patients) => {
    if(err) return res.json({status: 500, message: err})
    return res.json({status: 200, user: patients})
})

谢谢。

【讨论】:

    猜你喜欢
    • 2018-11-17
    • 2018-07-05
    • 2022-01-13
    • 2018-02-03
    • 1970-01-01
    • 2014-03-22
    • 2015-09-19
    • 2014-03-20
    • 2016-08-09
    相关资源
    最近更新 更多