在我的 Node.js express 应用程序中,我只是设置了一个用户登录,这是我第一次经历这种自学。
我这里有这个功能,
var db = require('mongojs').connect('localhost/busapp', ['users']),
crypto = require('crypto');
exports.new = function(req, res) {
function User(email, password) {
this.email = email;
this.password = password;
}
var user = new User(req.body.user.email, req.body.user.password);
console.log(user.email + user.password);
db.users.find({'email':user.email}, function(err, userFound) {
if (err) {
console.log('We have an error :(');
} else if (userFound === 'undefined') {
console.log('We couldn\'t find the user ' + user.email);
return;
} else {
console.log('We have fixed the error, hooorraayyy!! ' + userFound[0].email)
}
});
}
但控制台日志显示为
We found the user! undefined
我不确定我做错了什么,如果有人能说明情况会很好,谢谢!
好的,所以我通过将我的代码更改为上面的代码来修复它,但现在我遇到了一个问题,如果我尝试搜索不存在的电子邮件,它会使我的整个应用程序崩溃并出现以下错误:
C:\Users\Gacnt\Desktop\Busapp\node_modules\mongojs\node_modules\mongodb\lib\mongodb\connection\server.js:529
throw err;
^
TypeError: Cannot read property 'email' of undefined
at C:\Users\Gacnt\Desktop\Busapp\routes\session.js:22:72
at C:\Users\Gacnt\Desktop\Busapp\node_modules\mongojs\node_modules\mongodb\lib\mongodb\cursor.js:159:16
at commandHandler (C:\Users\Gacnt\Desktop\Busapp\node_modules\mongojs\node_modules\mongodb\lib\mongodb\cursor.js:628
:16)
at null.<anonymous> (C:\Users\Gacnt\Desktop\Busapp\node_modules\mongojs\node_modules\mongodb\lib\mongodb\db.js:1709:
18)
at g (events.js:175:14)
at EventEmitter.emit (events.js:106:17)
at Server.Base._callHandler (C:\Users\Gacnt\Desktop\Busapp\node_modules\mongojs\node_modules\mongodb\lib\mongodb\con
nection\base.js:130:25)
at C:\Users\Gacnt\Desktop\Busapp\node_modules\mongojs\node_modules\mongodb\lib\mongodb\connection\server.js:522:20
at MongoReply.parseBody (C:\Users\Gacnt\Desktop\Busapp\node_modules\mongojs\node_modules\mongodb\lib\mongodb\respons
es\mongo_reply.js:132:5)
at null.<anonymous> (C:\Users\Gacnt\Desktop\Busapp\node_modules\mongojs\node_modules\mongodb\lib\mongodb\connection\
server.js:481:22)
PS C:\Users\Gacnt\Desktop\Busapp>