1

I am trying to get index of an array in a Meteor template each loop. I referred to this and this.

This is what I done:

Template.SpaceList.helpers({
    Spaces: function() {
        var cursor =  Spaces.find();   
        var array = _.map(cursor, function(doc, index) {
          doc.number = index + 1; 
          return doc;
        });
        return array;

    }

And this is the template:

<template name="SpaceList">
  <table >
   <tbody>

        {{#each Spaces}}
            <tr>
                <td>{{number}} <a href="{{pathFor 'SpaceDetails'}}" >{{title}}</a></td>
                <td><a href="{{pathFor 'EditSpace'}}" > Edit</a></td>       
            </tr>
        {{/each}}
    </tbody>
 </table>

</template>

But I getting this error:

Exception in template helper: TypeError: Cannot set property 'number' of null

What is the problem?

Community
  • 1
  • 1
dev-jim
  • 2,404
  • 6
  • 35
  • 61

1 Answers1

2

find does not return an array, but a cursor. You need to first use fetch before _.map knows how to handle this:

    var cursor =  Spaces.find().fetch(); 
Christian Fritz
  • 20,641
  • 3
  • 42
  • 71