我有一個findOne查詢,並且當我驗證它是否返回一個空文檔時,我會拋出一個錯誤,說'無法讀取屬性'用戶名'空值'。當我嘗試接取doc.username在如果(doc.username!)這happend {Node.js和mongoose(mongodb)錯誤無法讀取屬性null'
我的代碼:
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);
}
});
}
誰能請解釋我這裏發生了什麼?
謝謝!
你得到的錯誤是哪一行? –