使用mongoose无法执行到findOne回调函数中
发布于 10 年前 作者 NortonLee 7944 次浏览 最后一次编辑是 8 年前 来自 问答

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

9 回复

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

你的moogoose成功的连接到数据库了吗

@chloe 链接到了,测试代码如下:

var mongoose = require(‘mongoose’); var settings = require(’…/settings’);

var db = mongoose.createConnection(“mongodb://127.0.0.1:27017/coolde”);

db.on(‘error’, function(error){ console.log(error); });

db.on(‘open’,function(error){ if(error){ console.log(error); } console.log(‘connect success.’); });

可以输出:connect success.

model中增加module.exports = mongoose.model(‘User’,userSchema);

下面是我上述代码输出的User对象: { [Function: model] hooks: { _pres: {}, _posts: {} }, base: { connections: [ [Object], [Object] ], plugins: [], models: { User: [Circular] }, modelSchemas: { User: [Object] }, options: { pluralization: true } }, modelName: ‘User’, model: [Function: model], db: { base: { connections: [Object], plugins: [], models: [Object], modelSchemas: [Object], options: [Object] }, collections: { users: [Object] }, models: { User: [Circular] }, config: { autoIndex: true }, replica: false, hosts: null, host: null, port: null, user: null, pass: null, name: null, options: null, otherDbs: [], _readyState: 0, _closeCalled: false, _hasOpened: false, _listening: false }, discriminators: undefined, schema: { paths: { userName: [Object], password: [Object], _id: [Object], __v: [Object] }, subpaths: {}, virtuals: { id: [Object] }, nested: {}, inherits: {}, callQueue: [ [Object] ], _indexes: [], methods: {}, statics: {}, tree: { userName: [Object], password: [Object], _id: [Object], id: [Object], __v: [Function: Number] }, _requiredpaths: undefined, discriminatorMapping: undefined, _indexedpaths: undefined, s: { hooks: [Object], queryHooks: [Object] }, options: { id: true, noVirtualId: false, _id: true, noId: false, validateBeforeSave: true, read: null, shardKey: null, autoIndex: null, minimize: true, discriminatorKey: ‘__t’, versionKey: ‘__v’, capped: false, bufferCommands: true, strict: true, pluralization: true }, _events: {} }, collection: { collection: null, opts: { bufferCommands: true, capped: false }, name: ‘users’, conn: { base: [Object], collections: [Object], models: [Object], config: [Object], replica: false, hosts: null, host: null, port: null, user: null, pass: null, name: null, options: null, otherDbs: [], _readyState: 0, _closeCalled: false, _hasOpened: false, _listening: false }, queue: [], buffer: true } }

@XueQian 这样试了也不可以,我在用的时候,User对象是有值的

连接数据酷代码修改如下,便可运行成功: 由:var db = mongoose.createConnection(settings.url); 改为: mongoose.connect(settings.url); var db = mongoose.connection;

回到顶部