0

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!

6
  • 2
    No such user is found, but the test for "not found" isn't succeeding. user will either be null or a document Object. In either case,, user == '' will be false. null is only == to itself or undefined and an object compared to a string will be first converted to a string, resulting in -- "[object Object]" == "" // false. Commented Sep 27, 2015 at 4:13
  • What is prompt() in node.js code? Commented Sep 27, 2015 at 4:15
  • Sorry I'm not understanding, how would I correct this? Commented Sep 27, 2015 at 4:16
  • You might want to consider hashing the passwords. Commented Sep 27, 2015 at 4:21
  • Passwords are already hashed in the database, I have not hashed the users input yet to compare with database password. when I console.log the response from user I get, >{ _id: 560734a53e04afd4029ab020, username: 'archlinuxusa', password: '9cb1e3525d22f14efd', server: 'ftb', admin: true, __v: 0 } Commented Sep 27, 2015 at 4:25

2 Answers 2

2

The error message Cannot read property 'password' of null indicates that user is null. But the code is checking for empty string. Check for null instead or in addition. For example, instead of...:

} else if (user == '') {

...do something more like this:

} else if (! user) {

! user will be true if user is empty string or null or any falsy value.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay the issue isn't with the username spectrum. its with the password section of this code, if I could somehow just get the array of variables from user from the first section, so when the user is asked for his password with the prompt function it can successfully verify the response with what is in mongodb. here is my whole code jsfiddle.net/L3taw280
The error message indicates that user is null and that the program is blowing up when you try to check user.password because you are trying to read the password property of null which you cannot do. The error is that you think you are validating that user has been successfully retrieved, but it has not been.
1

There isn't necessarily anything wrong with the line that's throwing the error:

if (password1 == user.password) {

This is more so where the issue becomes certain. The root of the issue is a few lines up:

} else if (user == '') {
    console.log("This user was not found")

The error message is suggesting that user is null (because null cannot have properties like .password), meaning in this case that no document was found matching the query. But, the condition isn't catching this, allowing the function to continue to execute beyond it and attempt to read user.password when there isn't a user.

This is because null == '' is false. In JavaScript, the null value is only == to itself or undefined.

var user = null;
console.log(user == '');        // false

console.log(null == '');        // false
console.log(null == null);      // true
console.log(null == undefined); // true

Adjusting the condition to check for null specifically should resolve this:

} else if (user == null) {
    console.log("This user was not found")

4 Comments

When using this method, it breaks the whole script, when I debugged it, the server was able to find the username that the user entered, but for some reason even though the script was able to find my user in the database it sent me to "this user was not found", when it used to send me to the next prompt for the password
@ChristopherKemp Note that you appear to possibly have a typo in the query, having a space and colon as characters within the property name -- 'Username: '.
upon assessing the username issue in the query, and entering a known username in the table. Whats your name?>archlinuxusa scs >This user was not found
There was a few issues with my code, and my statements, the issue was related to the way I was assessing what you said above, and the way I was using the findOne function. thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.