这是代码,很简单:
usermodel.findOne({ user: req.params.id }, function (err, user1){
if (err) throw err;
console.log(user1.user);
if (req.session.user != user1.user) {
console.log('Different user!');
}
});
console.log
正确返回,但是,当user1.user
node.js 进入 if 语句时,我收到此错误:
/root/pix/node_modules/mongoose/lib/utils.js:434
throw err;
^
TypeError: Cannot read property 'user' of null
at Promise.<anonymous> (/root/pix/app.js:168:38)
at Promise.addBack (/root/pix/node_modules/mongoose/lib/promise.js:128:8)
at Promise.EventEmitter.emit (events.js:96:17)
at Promise.emit (/root/pix/node_modules/mongoose/lib/promise.js:66:38)
at Promise.complete (/root/pix/node_modules/mongoose/lib/promise.js:77:20)
at Query.findOne (/root/pix/node_modules/mongoose/lib/query.js:1600:30)
at exports.tick (/root/pix/node_modules/mongoose/lib/utils.js:429:16)
at Collection.findOne
(/root/pix/node_modules/mongodb/lib/mongodb/collection.js:975:5)
at Cursor.toArray (/root/pix/node_modules/mongodb/lib/mongodb/cursor.js:159:9)
at Cursor.each (/root/pix/node_modules/mongodb/lib/mongodb/cursor.js:199:11)
为什么会发生这种情况,我该如何解决?
谢谢提前!