0

I'm trying to get "true" from hash compare. but a problem occurs when I write a username that does not exist in database.For example: if there is no "jack.33" as username, it is throwing mysql error and stopping the server. How can i block this error or create a solution? Here is the error:

    throw err; // Rethrow non-MySQL errors
            ^
TypeError: Cannot read property 'password' of undefined


app.post("/login", function(request, response) {
        var username = request.body.username;
        var pass = request.body.pass;

        con.query("select password from user where username = " + "\"" + username + "\"",function (err, result) {
            if(err) throw err;
            var passer = result[0].password;
            bcrypt.compare(pass, passer, function (err, res) {
                console.log(res);
                if (res === true) {
                    response.send("success")
                }else{
                    response.send("nop")
                }
            });
        });
    });
5
  • 1
    You need to check that the result array actually has stuff in it. The query can succeed but produce zero rows. Commented Jul 19, 2017 at 1:00
  • It looks like you are throwing this error yourself immediately after the query, are you catching it anywhere? Commented Jul 19, 2017 at 1:01
  • @tadman I'm checking it with if condition, but I can't stop the error, is there any possibility that to stop code after if(result === null) condition? Commented Jul 19, 2017 at 1:16
  • @Jarek Kulikowski yes, you're right. I'm thinking about how to send response message and stop it. Commented Jul 19, 2017 at 1:19
  • Samuel has an answer here that explains what I'm talking about. You need to check two conditions, you only check one. Commented Jul 19, 2017 at 1:23

2 Answers 2

2

The error you got there was not thrown by your database driver.

TypeError: Cannot read property 'password' of undefined is a typical error raised when the code expects a property that don't exist for an object.

Your SELECT query can return 0 or more rows and when nothing is returned, I suspect your database driver is giving you an empty array []. Hence index 0 of [] would be undefined.

Now undefined.password will die with a similar error because the property password is not part of the undefined object.

Example:

var result = []; // Assume SELECT query returns 0 row.
result[0].password;

Other than checking whether err is defined. Your code will also have to cater for scenario where user don't exist.

A simple check like if result.length === 1 should fix your logic error.

You might wonder what if more than 1 row is returned? This is usually not possible unless you have a bad database design where usernames are not unique. In other words, you have a greater problem.

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

Comments

0

You can also catch all uncaught errors globally.

process.on('uncaughtException', function (err) {
    console.log('UNCAUGHT', err.stack);
});

2 Comments

This is a solution to keep working, how can I use if(result === null){ response.send("nop") } and stop the process?
@AhmetAzizBeşli process.exit();

Your Answer

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