使用mongoose无法执行到findOne回调函数中
发布于 13小时前 作者 NortonLee 56 次浏览 来自 问答

使用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’);

1 回复

各位大大们都没遇到过类似的问题吗?

回到顶部