无法在流星模板助手中设置 属性
Cannot set property in meteor template helper
我试图在每个循环中获取 Meteor 模板中的数组索引。
我提到了 this and 。
这是我做的:
Template.SpaceList.helpers({
Spaces: function() {
var cursor = Spaces.find();
var array = _.map(cursor, function(doc, index) {
doc.number = index + 1;
return doc;
});
return array;
}
这是模板:
<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>
但是我收到这个错误:
Exception in template helper: TypeError: Cannot set property 'number'
of null
问题是什么?
find
不是 return 一个数组,而是一个游标。在 _.map
知道如何处理之前,您需要先使用 fetch
:
var cursor = Spaces.find().fetch();
我试图在每个循环中获取 Meteor 模板中的数组索引。
我提到了 this and
这是我做的:
Template.SpaceList.helpers({
Spaces: function() {
var cursor = Spaces.find();
var array = _.map(cursor, function(doc, index) {
doc.number = index + 1;
return doc;
});
return array;
}
这是模板:
<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>
但是我收到这个错误:
Exception in template helper: TypeError: Cannot set property 'number' of null
问题是什么?
find
不是 return 一个数组,而是一个游标。在 _.map
知道如何处理之前,您需要先使用 fetch
:
var cursor = Spaces.find().fetch();