使用mongoose链接数据库,在controller层执行findOne查询时,执行不到callback中,求大神解释下怎么回事?
controller中代码如下: var models = require(‘…/models’); var crypto = require(‘crypto’); var User = models.User;
exports.login = function(req, res){
var md5 = crypto.createHash('md5');
var password = md5.update(req.body.password).digest('base64');
User.findOne({'userName': req.body.username}, function(err, user){
console.log(111111);
if(err){
console.log(111111);
console.log(err);
}
if(!user){
return res.render('sign/login', { error: '用户不存在。' });
}
if(user.password != password && user.password != req.body.password){
req.flash('error', '用户密码错误。');
return res.redirect('login');
}
req.session.user = user;
res.redirect('manager');
});
};
User model代码如下: var mongoose = require(‘mongoose’); var Schema = mongoose.Schema;
var userSchema = new Schema({
userName: {type: String},
password: {type: String}
});
mongoose.model('User', userSchema);
初始化mongoose链接代码如下: var mongoose = require(‘mongoose’); var settings = require(‘…/settings’);
var db = mongoose.createConnection(settings.url);
db.on('error', function(error){
console.log(error);
});
// models require(‘./user’); exports.User = mongoose.model(‘User’);