Nenad M
Nenad M

Reputation: 139

NodeJS Express & Mongoose: How to get data from array not in one but in multi table columns

I have a list in mongodb that I use in another page to put numbers within array in each of that list collection. For example:

I have input list this where all, valid and invalid inputs are normal but first, second and third list are from loop so it's dynamic and I put in models to to be in array:

allVotes: {
    type: Number,
    required: true
},

validVotes: {
    type: Number,
    required: true
},

invalidVotes: {
    type: Number,
    required: true
},

partyVotes: {
    type: [Number], // this goes as array
    required: true
}

enter image description here

so in my mongodb compass it's look like this:

enter image description here

and now, when I want to make table of this content I want to data from this array shows in separate column as list from loop look like, not in one column but that what it shows me:

enter image description here

I presume that maybe instead of array it should be Schema.Types.ObjectId and to be refer to that list, I don't know.

This is code where I get all that with array:

router.get('/', (req, res)=>{
        StateResult.count().then(stateResultListCount=>{
                    res.render('admin/stateResultsLists/index', {
                        stateResultsLists: stateResultsLists,
            });
        });
    });
});

and in view this is tbody of table:

  <tbody>
    {{#each stateResultsLists}}
      <tr>
        <td>{{allVotes}}</td>
        <td>{{validVotes}}</td>
        <td>{{invalidVotes}}</td>
        <td>{{partyVotes}}</td>

    {{/each}}
  </tbody>

So, the simple question is how to get 5 to be in first list, 2 in second and 2 in third list?

Upvotes: 1

Views: 191

Answers (1)

wlh
wlh

Reputation: 3515

I think the solution you are looking for involves iterating over your partyVotes array. You need to have a similar conditional to iterate of your <th> elements to be the length of the partyVotes array, but I'll assume you will handle that elsewhere:

<tbody>
    {{#each stateResultsLists}}
        <tr>
            <td>{{allVotes}}</td>
            <td>{{validVotes}}</td>
            <td>{{invalidVotes}}</td>
            {{#each partyVotes}}
                <td>{{this}}</td>
            {{/each}}
       </tr>
    {{/each}}
</tbody>

Upvotes: 1

Related Questions