I’ve had this working before, but now it’s broken for some reason. I’m using MariaDB
as a database, and need to compare passwords, but it’s giving me an error Unexpected Identifier on "bcrypt" after await
. When I remove the await
, it works, but even if the password is wrong. What am I doing wrong here? Thanks
Edit: I forgot to include the user info after .then. I must of deleted it with some comments when I posted this question. I added it back. Here is my code:
JavaScript
x
54
54
1
//data connection pool
2
const pool = require('../models/db');
3
const { handleError, ErrorHandler } = require('../helpers/error');
4
const bcrypt = require('bcrypt');
5
6
module.exports = (req, res) => {
7
//destructuring assignment👇
8
const {user_name, password} = req.body;
9
let hashedPassword;
10
//TODO: hash password
11
12
var sql = `SELECT * FROM Govt_profiles WHERE
13
(user_name = ?)`;
14
15
//pool.query() is shortcut for pool.getConnection() + connection.query() + connection.release()
16
pool.query(sql, [user_name], async (err, data) => {
17
})
18
.then(rows => {
19
20
const user = rows.find( user => user['User_Name'] === user_name);
21
if (user == null) {
22
return res.status(400).send('Cannot find user');
23
}
24
25
try {
26
if (await bcrypt.compare(password, user['Password'])) {
27
console.log('Password: ', password, 'hashedPassword: ', hashedPassword);
28
console.log("this is a loginUser: ");
29
console.log(req.session);
30
//return res.redirect('questions')
31
return res.render('user-progress', {
32
userName: req.session.user_name,
33
attempts: req.session.attempts,
34
grade: req.session.grade
35
})
36
}
37
} catch(e) {
38
console.log("something broke: ", e);
39
res.status(500).send();
40
}
41
42
})
43
.catch(err => {
44
console.log(err.message);
45
console.log("hey couldn't find user");
46
req.session.error = new ErrorHandler(404, 'Incorrect email or last name');
47
res.render('login_user', {
48
//err: err (or if same name to this)
49
//err
50
error: req.session.error
51
});
52
})
53
}
54
I tried Lucas suggestion, and got this error:
JavaScript
1
17
17
1
C:usersdanieldocumentsgitusa-govt-quizcontrollersloginUser.js:41
2
const isValidPsw = await bcrypt.compare(password, user['Password']);
3
^^^^^
4
5
SyntaxError: await is only valid in async function
6
at wrapSafe (internal/modules/cjs/loader.js:979:16)
7
at Module._compile (internal/modules/cjs/loader.js:1027:27)
8
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
9
at Module.load (internal/modules/cjs/loader.js:928:32)
10
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
11
at Module.require (internal/modules/cjs/loader.js:952:19)
12
at require (internal/modules/cjs/helpers.js:88:18)
13
at Object.<anonymous> (C:usersdanieldocumentsgitusa-govt-quizserver.js:5:29)
14
at Module._compile (internal/modules/cjs/loader.js:1063:30)
15
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
16
[nodemon] app crashed - waiting for file changes before starting
17
Any other ideas?
Advertisement
Answer
I was able to resolve this by putting a “async” in front of rows. This seems strange to me, and I don’t know if this is normal, but it works.
Here’s my final code from what I modified:
JavaScript
1
37
37
1
.then(async rows => {
2
3
const user = rows.find( user => user['User_Name'] === user_name);
4
if (user == null) {
5
return res.status(400).send('Cannot find user');
6
}
7
8
try {
9
if (await bcrypt.compare(password, user['Password'])) {
10
console.log('Password: ', password, 'hashedPassword: ', hashedPassword);
11
console.log("this is a loginUser: ");
12
console.log(req.session);
13
//return res.redirect('questions')
14
return res.render('user-progress', {
15
userName: req.session.user_name,
16
attempts: req.session.attempts,
17
grade: req.session.grade
18
})
19
}
20
} catch(e) {
21
console.log("something broke: ", e);
22
res.status(500).send();
23
}
24
25
})
26
.catch(err => {
27
console.log(err.message);
28
console.log("hey couldn't find user");
29
req.session.error = new ErrorHandler(404, 'Incorrect email or last name');
30
res.render('login_user', {
31
//err: err (or if same name to this)
32
//err
33
error: req.session.error
34
});
35
})
36
}
37