index.js里面的代码如图 router.route(’/login’) .get(function (req,res) { res.render(‘login’, { title: ‘Express’ }); }) .post(function (req,res) { //var name=req.body.name; //var psw=req.body.psw; var newUser = new User({ name: req.body.name, psw: req.body.psw //email: req.body.email });
console.log(req.body.name);
User.get(newUser.name,function (err, user) {
console.log('123');
if(!user){
req.flash('error','用户不存在!');
return res.redirect('/bad-request');
}
if(req.body.psw!=user.psw){
req.flash('error','用户密码不正确!');
return res.redirect('/bad-request');
}
req.session.user=user;
req.flash('success','用户登陆成功');
res.redirect('/success');
})
});
封装的数据库utils如下:
User.get=function(name,callback) {
mongodb.open(function(err,db) {
if(err){
return callback(err);
}
//获取user集合
db.collection('user',function(err,collection) {
if(err){
mongodb.close();
return callback(err);
}
//查找用户名name值为name文档
collection.findOne({
name:name
},function(err,doc) {
mongodb.close();
if(doc){
var user=new User();
callback(err,user);//成功!返回查询的用户信息
}else{
callback(err,null);//失败!返回null
}
});
});
});
};
报错如下: /Users/yanrui/project/rayblog/node_modules/mongodb/lib/utils.js:98 process.nextTick(function() { throw err; }); ^
TypeError: Cannot read property ‘name’ of undefined at new User (/Users/yanrui/project/rayblog/models/user.js:4:19) at /Users/yanrui/project/rayblog/models/user.js:53:30 at handleCallback (/Users/yanrui/project/rayblog/node_modules/mongodb/lib/utils.js:96:12) at /Users/yanrui/project/rayblog/node_modules/mongodb/lib/collection.js:1357:5 at handleCallback (/Users/yanrui/project/rayblog/node_modules/mongodb/lib/utils.js:96:12) at /Users/yanrui/project/rayblog/node_modules/mongodb/lib/cursor.js:661:5 at handleCallback (/Users/yanrui/project/rayblog/node_modules/mongodb-core/lib/cursor.js:159:5) at nextFunction (/Users/yanrui/project/rayblog/node_modules/mongodb-core/lib/cursor.js:683:5) at /Users/yanrui/project/rayblog/node_modules/mongodb-core/lib/cursor.js:585:7 at queryCallback (/Users/yanrui/project/rayblog/node_modules/mongodb-core/lib/cursor.js:220:18) at Callbacks.emit (/Users/yanrui/project/rayblog/node_modules/mongodb-core/lib/topologies/server.js:119:3) at .messageHandler (/Users/yanrui/project/rayblog/node_modules/mongodb-core/lib/topologies/server.js:358:23) at Socket.<anonymous> (/Users/yanrui/project/rayblog/node_modules/mongodb-core/lib/connection/connection.js:292:22) at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at readableAddChunk (_stream_readable.js:172:18)
新手。。求教