I have a login in an web app, which reads from the MongoDB and sorts it into the sorted array using lodash. The problem is that my console.log("sorted in /api/login:", sorted);
for some reason returns nothing, which doesn't make console.log
output yeet... How do I fix this? I already tried putting some more awaits, but that didn't help me. What is this promise alignment everyone is talking about? I can't find any info online about it..
var express = require('express');
const app = express();
const User = require('./models/user.js');
var _ = require('lodash');
const sorted = [];
User.find({},{ firstname: 1, password: 1 }, async function(err, users) {
const flattenUsers = _(users).map(({firstname, password}) => ([firstname, password])).flatten().value();
console.log(flattenUsers);
const sorted = await flattenUsers.reduce((a, e, i) => (i % 2 || a.push([]), a[a.length - 1].push(e), a), []);
await console.log("sorted:", sorted);
});
app.post('/api/login', apiLimiter, async function (req, res){
try {
console.log("sorted in /api/login:", sorted);
console.log("Req firstname: ",req.body.firstname)
const hashedPassword = await bcrypt.hash(req.body.password, 10);
console.log("Hashed pw: ", hashedPassword);
console.log(await bcrypt.compare('testtest',hashedPassword));
// const user = { id: req.body.id, username: req.body.username, password: req.body.password };
var user = new User({firstname: req.body.firstname, eMail: req.body.eMail, password: hashedPassword });
sorted.forEach(async ([eMail, password]) => {
await bcrypt.compare(password, users[eMail]).then((e, r) => {
// r = true if hash = hashed pw
console.log("Yeet");
});
});
jwt2.sign({user}, 'secrethere', { expiresIn: '15min'}, (err, token) =>{
res.json({
token
});
});
} catch (err) {
res.status(500).send()
console.log(err);
}
});
My user.js:
const mongoose = require('mongoose');
const userSchema = mongoose.Schema({
firstname: {
type: String,
required: true,
unique: true
},
lastname: String,
eMail: {
type: String,
required: true,
unique: true
},
password: String,
active: Boolean
});
module.exports = mongoose.model("User", userSchema);
Update
Errors I get when implementing the recommended answer:
(node:17080) UnhandledPromiseRejectionWarning: ReferenceError: users is not defined at C:\Users\User\Documents\Carina\Canopus\src\app.js:252:38 at Array.forEach () at C:\Users\User\Documents\Carina\Canopus\src\app.js:251:12 (node:17080) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:17080) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. (node:17080) UnhandledPromiseRejectionWarning: ReferenceError: users is not defined at C:\Users\User\Documents\Carina\Canopus\src\app.js:252:38 at Array.forEach () at C:\Users\User\Documents\Carina\Canopus\src\app.js:251:12 (node:17080) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2) (node:17080) UnhandledPromiseRejectionWarning: ReferenceError: users is not defined at C:\Users\User\Documents\Carina\Canopus\src\app.js:252:38 at Array.forEach () at C:\Users\User\Documents\Carina\Canopus\src\app.js:251:12 (node:17080) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
Update2
(node:11252) UnhandledPromiseRejectionWarning: Error: data and hash arguments required at Object.compare (C:\Users\User\Documents\Carina\Canopus\node_modules\bcrypt\bcrypt.js:209:17) at C:\Users\User\Documents\Carina\Canopus\node_modules\bcrypt\promises.js:29:12 at new Promise () at Object.module.exports.promise (C:\Users\User\Documents\Carina\Canopus\node_modules\bcrypt\promises.js:20:12) at Object.compare (C:\Users\User\Documents\Carina\Canopus\node_modules\bcrypt\bcrypt.js:205:25) at C:\Users\User\Documents\Carina\Canopus\src\app.js:252:20 at Array.forEach () at C:\Users\User\Documents\Carina\Canopus\src\app.js:251:12 (node:11252) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:11252) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. (node:11252) UnhandledPromiseRejectionWarning: Error: data and hash arguments required at Object.compare (C:\Users\User\Documents\Carina\Canopus\node_modules\bcrypt\bcrypt.js:209:17) at C:\Users\User\Documents\Carina\Canopus\node_modules\bcrypt\promises.js:29:12 at new Promise () at Object.module.exports.promise (C:\Users\User\Documents\Carina\Canopus\node_modules\bcrypt\promises.js:20:12) at Object.compare (C:\Users\User\Documents\Carina\Canopus\node_modules\bcrypt\bcrypt.js:205:25) at C:\Users\User\Documents\Carina\Canopus\src\app.js:252:20 at Array.forEach () at C:\Users\User\Documents\Carina\Canopus\src\app.js:251:12 (node:11252) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2) (node:11252) UnhandledPromiseRejectionWarning: Error: data and hash arguments required at Object.compare (C:\Users\User\Documents\Carina\Canopus\node_modules\bcrypt\bcrypt.js:209:17) at C:\Users\User\Documents\Carina\Canopus\node_modules\bcrypt\promises.js:29:12 at new Promise () at Object.module.exports.promise (C:\Users\User\Documents\Carina\Canopus\node_modules\bcrypt\promises.js:20:12) at Object.compare (C:\Users\User\Documents\Carina\Canopus\node_modules\bcrypt\bcrypt.js:205:25) at C:\Users\User\Documents\Carina\Canopus\src\app.js:252:20 at Array.forEach () at C:\Users\User\Documents\Carina\Canopus\src\app.js:251:12 (node:11252) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
Update 3
Now I get these errors:
(node:14268) UnhandledPromiseRejectionWarning: Error: data and hash arguments required at Object.compareSync (C:\Users\User\Documents\Carina\Canopus\node_modules\bcrypt\bcrypt.js:167:15) at C:\Users\User\Documents\Carina\Canopus\src\app.js:252:20 at Array.forEach () at C:\Users\User\Documents\Carina\Canopus\src\app.js:251:12 (node:14268) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:14268) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. (node:14268) UnhandledPromiseRejectionWarning: Error: data and hash arguments required
at Object.compareSync (C:\Users\User\Documents\Carina\Canopus\node_modules\bcrypt\bcrypt.js:167:15) at C:\Users\User\Documents\Carina\Canopus\src\app.js:252:20 at Array.forEach () at C:\Users\User\Documents\Carina\Canopus\src\app.js:251:12 (node:14268) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2) (node:14268) UnhandledPromiseRejectionWarning: Error: data and hash arguments required
at Object.compareSync (C:\Users\User\Documents\Carina\Canopus\node_modules\bcrypt\bcrypt.js:167:15) at C:\Users\User\Documents\Carina\Canopus\src\app.js:252:20 at Array.forEach () at C:\Users\User\Documents\Carina\Canopus\src\app.js:251:12 (node:14268) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)