我有一个 findOne 查询,当我验证它是否返回一个空文档时,我会收到一条错误消息,提示“无法读取属性 'username' of null”。当我尝试在 if(!doc.username) {
我的代码:
function checkAccDb(username, password) { console.log(2);
/* Check if accounts exists in db */
db.findOne({username: username}, function(err, doc){ console.log(3);
if(err) throw err;
if(!doc.username) {
add2stack(username, password);
}
else if(doc.status == 200) {
end(username, password, 1000);
}
else if(doc.status == 401) {
if(doc.password == password)
end(username, password, 401);
else
add2stack(username, password);
}
else {
add2stack(username, password);
}
});
}
谁能解释一下这里发生了什么?
谢谢!