0
let allDB = await this.findAll();

//allDB output in console shows it has everything from db as I want

allDB .forEach(item => {

  //item is undefined here
});

async findAll() {

    return this.db.find()

}

I'm trying to load everything from a mongo collection and after that do a foreach loop trought it, but item is always undefined. Thanks for your help

CarlosMagalhaes
  • 91
  • 2
  • 10
  • Hi, `forEach` doesn't work inside async methods. Consider using a normal `for` loop. Check this [so thread](https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop) – SakisTsalk Sep 14 '21 at 08:19

1 Answers1

0

In the code you provided us there is a space between 'allDB' and '.forEach'. Is it the case in your project ? Because it can be the source of your error. Remove it to be sure it's not this.

I don't think that's the problem because it should throw a syntax error but it's still worth trying.

Valentin Roche
  • 284
  • 2
  • 5