0

Hello I am doing a website and I use mongoose for stock user data and I would like to get like a "dashboard" with all user and her id. I have all the data in a "json file" and I would like to obtain it and make it visible on the page

 {
      '609e424a164ebb6c207856fe': {
        role: 'admin',
        _id: 609e424a164ebb6c207856fe,
        name: 'Johann',
        email: 'private@gmail.com',
        password: 'some encrypt password',
        date: 2021-05-14T09:26:34.924Z,
        __v: 0
  },
  '609e99cd0f0cd21d4415b802': {
    role: 'basic',
    _id: 609e99cd0f0cd21d4415b802,
    name: 'Test Johann',
    email: 'private@gmail.com',
    password: 'an other encrypt password',
    date: 2021-05-14T15:39:57.282Z,
    __v: 0
  }
}

And I only want the name and the id (and why note role if its possible) my website language is in ejs and for get these data I just use <% console.log(data) %> that give me all of the db

JohannC
  • 41
  • 1
  • 6
  • Does this answer your question? [How do you do a for loop/for each in EJS?](https://stackoverflow.com/questions/49264980/how-do-you-do-a-for-loop-for-each-in-ejs) – Kunal Mukherjee May 14 '21 at 18:01

2 Answers2

0

Im guessing you did this in your route to pull an array

const stocks = await stock.find() //to pull without any condition tho
res.render('yourPage.ejs', {stocks}) // render page with data[]

so in yourPage.ejs you can do this

<ul>
  <% for(stock of stocks){%>
  <li>
   <span><%= stock.name%></span>
   <span><%= stock._id%></span>
  </li>
  <%}%>
</ul>
  • i didn't do this because I couldn't use await and any async thing (btw I don't know why and i still try to fix that) so here is my route router.get('/utilisateurs',ensureAuthenticated,(req,res)=>{ User.find({}, function(err, users) { var userMap = {}; users.forEach(function(user) { userMap[user._id] = user; }); res.render('user',{ user: req.user, data: userMap}); }) }) – JohannC May 14 '21 at 18:46
0

ok I find it by myself x)

So because async didn't work I get error and I just find that https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Errors/is_not_iterable that really helps me :o and I use what's @Ekama Uloho give me to do that

<ul>
            <% for(let country of Object.keys(data)){
                const capital = data[country].name%>
            <li>
             <span><%= country%></span>
             <span>|    <%= capital%></span>
            </li>
            <%}%>
          </ul>
JohannC
  • 41
  • 1
  • 6