0

I have the following code:

User.find({},{ firstname: 1, password: 1 }, function(err, users) {
  const flattenUsers = _(users).map(({firstname, password}) => ([firstname, password])).flatten().value();
  console.log(flattenUsers);
  const sorted = flattenUsers.reduce((a, e, i) => (i % 2 || a.push([]), a[a.length - 1].push(e), a), []);
});

I would like to put the variable sorted out in the global scope, how do I do that? I tried returning but that's not an option it seems, public is not supported in JavaScript, my IDE says it's a TS only thing. What should I do to accomplish my task?

Munchkin
  • 857
  • 5
  • 24
  • 51
  • 3
    Your problem is not a *scope* problem. The issue is that `User.find()` is *asynchronous*. The callback function is invoked only after the initiated operation completes. – Pointy Dec 04 '19 at 13:34
  • You can just wrap your code in a promise and resolve it when ready. If you don't want to be stuck inside the callback, use async/await. – Kobe Dec 04 '19 at 13:41

0 Answers0