1

I'm new to the MEAN stack and I'm having trouble printing out an HTML table using EJS.

Basically, I have an EJS tag that stays white inside of VS Code and I have no idea why (line 14).

enter image description here

I have two forEach loops, the first one on line 6 outputs my mongoDB cursor results correctly. However, the second forEach loop with the HTML does not work because I cannot close my EJS tag for some reason and it breaks everything from that place down.

How do you correctly generate an HTML table with a forEach loop in EJS while using a MongoDB cursor object?

Just in case you want to see how I am getting my data (the console.logs can be seen in the first image):
enter image description here

Kurt Leadley
  • 513
  • 3
  • 20
  • `db.collection('employees').find()` is an asynchronous function. This is a duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – jeffheifetz Sep 26 '19 at 13:36
  • @jeffheifetz I'm confused because I thought I had successfully passed the response of `db.collection('employees').find()` from `app.js` to `list.js`. Shouldn't employees just be an object I can iterate over regularly now? As seen on lines 6 to 8 I can `console.log` all my results in a forEach loop, but I can't spit out HTML in a forEach loop and I don't get why. – Kurt Leadley Sep 26 '19 at 14:57

1 Answers1

1

I changed the app.get block of code to:

enter image description here

I think the main difference is that I put the response.render code inside of the .find() method ? I believe the comment Jeff made has the truth behind it. I don't fully understand it yet, but I know now that you want to pass the query object inside of the .find() method now. I also still don't know why I could console.log, but not embed HTML before the fix. That is strange, however, by putting the response in the .find(), I can do both now. Go figure.

One note, the syntax highlighting is still messed up in the ejs on the right panel, but it comes out clean in the browser now. Here is the rest of the code and some screen shots: enter image description here

enter image description here

Kurt Leadley
  • 513
  • 3
  • 20