0

The mysql query always returns undefined and I don't know whats wrongs. It used to work before I added the async await to the functions but that used to return an error in the terminal because bcrypt is an async function. Could someone help me with this? Here is my code

app.post('/login', async function(req, res) {
    let username = req.body.username;
    let password = req.body.password;

    if (username && password) {
        connection.query('SELECT username, password, email FROM accounts WHERE username = ?', [username], async function(err, result, fields) {
            if (result.length > 0) {
                try {
                    if(await bcrypt.compare(password, result.password)) {
                        req.session.loggedin = true;
                        req.session.username = username;
                        res.redirect('/');
                    }
                    else {
                        res.send('Incorrect Username and/or Password!');
                    }
                }
                catch (err) {
                    //res.status(500).send();
                    res.send('Something wrong happened');
                    console.log(err);
                }
            } else {
                res.send('Incorrect Username and/or Password!');
            }           
            res.end();
        });
    } else {
        res.send('Please enter Username and Password!');
        res.end();
    }
});
  • Maybe you get more traffic from JS specialists if you change the title to reflect it is more of a JS problem, like "Implementation of bcrypt/async gives query-result of undefined". Like you said: the query worked before. – Sam020 Aug 05 '21 at 11:02
  • @Sam020 Changed – user_9987654321 Aug 05 '21 at 11:09
  • are you sure you're getting the password from the query in `result.password`? – Srinath Kamath Aug 05 '21 at 11:18
  • @SrinathKamath I checked with the mysql client and I got it. There must be a problem with the code – user_9987654321 Aug 05 '21 at 11:21
  • What is it? Please start using `mysql2` rather than `mysql` for implementing MySQL in Nodejs. Checkout my post (https://stackoverflow.com/questions/25344661/what-is-the-difference-between-mysql-mysql2-considering-nodejs/63722618#63722618) & (https://stackoverflow.com/questions/64464555/unnesting-node-database-calls/65138795#65138795) – Srinath Kamath Aug 05 '21 at 11:26
  • @SrinathKamath I've changed to mysql2 and also changed the code to [this](https://pastebin.com/2hxtnHUZ) but now it gives me `cannot read property 'length' of undefined` – user_9987654321 Aug 05 '21 at 11:56
  • Please go through this. I have corrected it for you. (https://pastebin.com/JAG3Z6jm) Also learn more about async await and try catch to manage errors and also the mysql2 docs – Srinath Kamath Aug 05 '21 at 14:45
  • @SrinathKamath thanks it worked. Although it did initially return `ReferenceError: Cannot access 'controller' before initialization` so I had to move the `app.post('/login', controller);` at the bottom. – user_9987654321 Aug 05 '21 at 16:39
  • That's Cool! keep coding. – Srinath Kamath Aug 05 '21 at 18:49

0 Answers0