我正在查询一个 mongo 集合,但我试图访问的属性不可用,因为该对象显然是未定义的。我正在使用回调,所以我不确定它为什么要尝试访问空对象?
exports.checkPermissions = function(uname, pw){
var MongoClient = require('mongodb').MongoClient,
format = require('util').format;
console.log(uname + ", " + pw);
MongoClient.connect('mongodb://localhost/users', function(err, udb) {
if(err) throw err;
var targetcol = udb.collection('profiles');
targetcol.find({username: uname, password: pw}, {permissions: 1}).toArray(function(err, results) {
if( err || !results){
console.log("error occured");
response.redirect('/');
}
else {
if (results[0].permissions == 'system admin'){
response.redirect('/adminDashboard');
}
else
response.redirect('/userDashboard');
}
udb.close();
});
});
}
我收到错误的部分是:
if (results[0].permissions == 'system admin')
events.js:72
throw er; // Unhandled 'error' event
^
TypeError: Cannot read property 'permissions' of undefined