I am using mongodb and mongoose in node.js Inside the function, it verifies the username and password. user.password works inside the first if statement, but after that in the if else below, where the user actually comes into play it returns.
if (password1 == user.password) {
^
TypeError: Cannot read property 'password' of null
The code that I have is.
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");
})
}
})
Anyone know how to fix this issue
Thank you!
useris found, but the test for "not found" isn't succeeding.userwill either benullor a documentObject. In either case,,user == ''will be false.nullis only==to itself orundefinedand an object compared to a string will be first converted to a string, resulting in --"[object Object]" == "" // false.prompt()in node.js code?