【发布时间】:2015-09-27 04:10:23
【问题描述】:
我在 node.js 中使用 mongodb 和 mongoose 在函数内部,它验证用户名和密码。 user.password 在第一个 if 语句中起作用,但之后在下面的 if else 中,用户实际发挥作用的地方它返回。
if (password1 == user.password) {
^
TypeError: Cannot read property 'password' of null
我的代码是。
User.findOne({ 'Username: ': username1}, function(err, user) {
if (err){
console.log("There was an error proccesing the request".red + " : ".red + err);
} else if (user == '') {
console.log("This user was not found")
} else {
prompt('Password: ', function(password1){
if (password1 == user.password) {
console.log("User Login Sucsess")
} else {
console.log("Password incorrect")
proccess.exit();
}
console.log("made it");
})
}
})
任何人都知道如何解决此问题
谢谢!
【问题讨论】:
-
找不到这样的
user,但“未找到”的测试没有成功。user将是null或文档Object。在任何一种情况下,user == ''都是错误的。null仅是==对自身或undefined并且与字符串比较的对象将首先转换为字符串,从而产生 --"[object Object]" == "" // false。 -
node.js 代码中的
prompt()是什么? -
对不起,我不明白,我该如何纠正这个问题?
-
您可能需要考虑对密码进行哈希处理。
-
密码已经在数据库中进行了哈希处理,我还没有对用户输入的输入进行哈希处理以与数据库密码进行比较。当我 console.log 收到用户的响应时,>{ _id: 560734a53e04afd4029ab020, username: 'archlinuxusa', password: '9cb1e3525d22f14efd', server: 'ftb', admin: true, __v: 0 }
标签: javascript node.js mongodb if-statement