0

I have an ejs file that creates a table for all the users in my mongodb. Right now the ejs file simply creates 1 row for every user in my database. What I need it to do instead is create several rows for each user based on how many bullets they have. Each User has a bullets property, and the query is only pulling users that have bullets.

So if Johnny Cash had 5 Bullets instead of creating just one new row for Johnny Cash, it would create 5 new table rows for him. Ideally, these rows would be labled Johnny Cash (1), Johnny Cash (2), Johnny Cash (3), and so on.

dashboard.js

<%- include('partials/header'); %>
<a href="/api/users/logout" class="btn btn-secondary">Logout</a>
<a href="/api/users/buyBullet" class="btn btn-warning">Buy Bullet</a>
<div class="container">
  <div class="col-md-6 m-auto">
    <div class="card card-body">
      <h1 class="text-center mb-3">Dashboard</h1>
      <p>There is a total of <%= users.length %> users with a total of <%= bullets %> bullets</p>
      <p>The Date is now
        <b><%= new Intl.DateTimeFormat('en-GB', { year: 'numeric', month: 'long', day: '2-digit'}).format(new Date()) %></b>
        and the time is now <b><%= new Date().toLocaleTimeString() %></b></p>
    </div>
  </div>

  <div class="mt-5 mb-5">
      <% users.forEach(function(user){ %>
        <% user.bullets.forEach(function(user2){ %>
          <p><%= user2.name %></p>
        <% }); %>
      <% }); %>
  </div>

  
  
</div>

indexRouter.js (this is where the users object is defined)

const express = require('express');
const router = express.Router();
const User = require('../user/userModel');
const { ensureAuthenticated } = require('../../config/auth');
const _ = require('lodash');


router.get('/dashboard', ensureAuthenticated, async (req, res, next) => {
  const users = await User.find({ bullets: { $gt: 0 } });
  let bulletsArr = [];

  users.forEach(user => {
   bulletsArr.push(user.bullets);
  })

  const bullets = _.sum(bulletsArr);

  res.render('dashboard', {
    users,
    bullets
  });
});

module.exports = router;

You can also check the repo!

Github REPO

Jordan
  • 176
  • 10
  • Does this answer your question? [Loop through JSON in EJS](https://stackoverflow.com/questions/22952044/loop-through-json-in-ejs) – zS1L3NT Feb 09 '21 at 07:45
  • 1
    Just add a second loop inside the first one on property `user.bullets` and render one row for each of the bullet items. – Abrar Hossain Feb 09 '21 at 23:45
  • @AbrarHossain I have changed the code above to include a nested loop but this is rendereing my error.ejs with no error message. I am not exactly sure why it isnt working. – Jordan Feb 10 '21 at 00:47
  • 1
    Have you tried linting your script? Most likely reasons are syntax error. One suggestion would be to try to see if `user.bullets` and `user2` are `undefined` or not at the start. Include some debug statements to see that. Lastly, you may try using `<%- %>` instead of `<=` in your code where you are setting `user2.name`. – Abrar Hossain Feb 10 '21 at 01:38

0 Answers0