-1

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)

1

1 Answer 1

1

Your problem is that when you call a route in express that is function that starts from app.post('/api/login', apiLimiter, async function (req, res).

So this code never gets called, just put this inside function (inside of try) and everything should work

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);
});

UPDATE, working code with function

var express = require('express');
const app = express();
const User = require('./models/user.js');
var _ = require('lodash');

async function getSorted(){
const sorted = [];
let users = await User.find({},{ firstname: 1, password: 1 });
  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), []);
  console.log("sorted:", sorted);
return Promise.resolve([sorted,users])
}
app.post('/api/login', apiLimiter, async function (req, res){


  try  {
  let [sorted,users] = await getSorted();
    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]) => {
      let result = bcrypt.compareSync(password, users[eMail]);
        // result = 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);
  }
});
Sign up to request clarification or add additional context in comments.

14 Comments

Yeet doesn't get printed out :/ and the console.log("sorted in /api/login:", sorted); still prints out nothing probably because it's out of scope
Yeah, now the problem is because User.find is promise so you will need to put await before User.find.
Can you edit the answer accordingly? It throws me a syntax error the way you wrote it
@Munchkin here you go.
changed const to var now I get UnhandledPromiseRejectionWarning: ReferenceError: users is not defined. Updated the question to contain the full list of errors P.S Yeet was also not printed out
|

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.