Blaze: Showing array items from collections

Hey guys,

Here is a little challenge I am facing… I have a collection which contains an array of 3 fields. I would like to show a table with this information, so I am trying to build a Template Helper and the appropriate {{ … }} on the html file.

Unfortunately, none of my approaches so far have worked. Here is my code:

collections.js

const PaymentsSchema = new SimpleSchema({
    userId: {
        type: String,
        optional: true
    },
    months: {
        type: Array,
        optional: true
    },
    'months.$': {
        type: String,
        optional: true
    },
    'months.$.price': {
        type: Number,
        optional: true
    },
    'months.$.status': {
        type: String,
        optional: true
    }
});

payments.js

Template.Payments.helpers({

        'payment'(){

            return Payments.findOne({
                        userId: userId
                    },{
                        fields:{
                            months: 1
                        }
            });

        }

    });

payments.html

<template name="Payments">
    <table class="highlight">
        <thead>
            <tr>
                <th>Month</th>
                <th>Price</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            {{#each payment}}
                <tr class="link-tabela">
                    <td>{{months}</td>
                    <td>{{price}}</td>
                    <td>{{status}}</td>
                </tr>
           {{/each}}
        </tbody>
    </table>
</template>

Any idea how I can accomplish this? Thanks!

Your payment() helper returns one object - so you can’t do a #forEach on it.

your HTML:

<template name="Payments">
    <table class="highlight">
        <thead>
            <tr>
                <th>Month</th>
                <th>Price</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            {{#each month in payment.months}}
                <tr class="link-tabela">
                    <td>{{@index}</td> // You don't seem to have a property that says what month it is
                    <td>{{month.price}}</td>
                    <td>{{month.status}}</td>
                </tr>
           {{/each}}
        </tbody>
    </table>
</template>

You’re using {{#each ...}} with findOne. That should throw an exception. Use find instead of findOne.

1 Like