Search code examples
javascripttemplatesmeteorcursor

How do I get the contents of an Array to reflect in my template?


My return db.Collection.find().fetch() cursor reflects all the elements correctly in the template, apart from the elements within an array.

How do I also get the contents of the Array to reflect in my template?

Following below are the db.Collection.find().fetch() results as seen in the browser console.

_id: "CJiuk6jjFEBLDQrQc"
postedDate: Wed Feb 15 2017 15:10:50 GMT+0300 (EAT)
descriptions: "Clothes"
viewStatisticsArray: Array[1]

Find below the viewStatisticsArray: Array[1] elements when expanded in the console.

0: Object
nrOfViews: 3096
statsDate: "Tue Mar 07 2017 14:10:56 GMT+0300 (EAT)"

And my template function:

  'list': function(){
        return buyList.find({}).fetch();
    }

Find below the results in my template:

{{#each list}}
Posted Date: {{postedDate}} displays: Wed Feb 15 2017 15:10:50 GMT+0300 (EAT)
Description: {{descriptions}} displays: Clothes
View Stats: {{viewStatisticsArray.nrOfViews}}  {{viewStatisticsArray.statsDate}} FAILS to DISPLAY ANYTHING

{{/each}}

How do I correctly access elements of the viewStatisticsArray array?

Looking forward to your help.


Solution

  • {{#each items}}
        <div>{{postedDate}}</div>
        <div>{{description}}</div>
        <div>
        {{#each viewStatisticsArray}}
            {{nrOfViews}}
            {{statsDate}}
        {{/each}}
        </div>
    {{/each}}
    

    @SirBT not sure how you did your iteration, but the above should surely work. If not the only reason may be viewStatisticsArray is not being sent by your publish function.