我查看了这个问题Meteor display array inside a collection,但解决方案对我不起作用。我有以下模板:
<template name="remesasList">
<table class="table table-sm" id="remesasList">
<thead>
<tr>
<th>Dia emitido</th>
<th>Nombre deudor</th>
<th>Nombre de Factura</th>
<th>Importe</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{{#each remesas}}
{{> remesa}}
{{/each}}
</tbody>
</table>
</template>
我从remesasList.js获取集合remesas:
Template.remesasList.onCreated(function remesasListOnCreated() {
var self = this;
self.autorun(function(){
remesasHandle = self.subscribe('remesas', {
onError: function (error) { console.log("onError", error.reason); }
});
self.state = new ReactiveDict();
self.state.set("currentdate", new Date().getTime());
});
});
Template.remesasList.helpers({
remesas() {
if( remesasHandle && remesasHandle.ready() ) {
const instance = Template.instance();
var currentdate = new Date(instance.state.get("currentdate"));
var firstDay = new Date(currentdate.getFullYear(), currentdate.getMonth(), 1);
var lastDay = new Date(currentdate.getFullYear(), currentdate.getMonth() + 1, 0);
console.log(firstDay);
moment.locale('es');
return Remesas.find({dia_emitido: {$gte: firstDay, $lte: lastDay}});
} else {
console.log("No hay remesas");
}
},
我为每个remesa迭代该集合:
<template name="remesa">
<tr id={{_id}} class="js-row-info">
<td>{{fecha_default dia_emitido}}</td>
<td>{{nombre_deudor}}</td>
<td>{{nombre_factura}}</td>
<td>{{importe_total}}</td>
<td>{{boolean_a_si_no pagado}}</td>
</tr>
<tr>
<td>
<div>
{{#each item in items}}
{{item.tipo}}
{{/each}}
</div>
</td>
</tr>
</template>
我有以下对象remesa,我想显示所有“tipo”字段:
{
"_id" : "qHDQypADCGodRDFt7",
"dia_emitido" : ISODate("2017-03-24T23:00:00Z"),
"nombre_deudor" : "asdsad",
"nombre_factura" : "asdsa",
"items" : [
{ "numeroItem" : 1, "tipo" : "Alquiler", "concepto" : ISODate("2017-02-28T23:00:00Z"), "importe" : 1111 },
{ "numeroItem" : 2, "tipo" : "Alquiler", "concepto" : ISODate("2017-02-28T23:00:00Z"), "importe" : 2222 },
{ "numeroItem" : 3, "tipo" : "Alquiler", "concepto" : ISODate("2017-09-30T22:00:00Z"), "importe" : 789 },
{ "nombre" : "Item 4", "tipo" : "Otro", "concepto" : "un concepto", "importe" : 124578 }
],
"numero_de_items" : 4,
"importe_total" : 128700
}
当我运行此代码时出现以下错误:
Exception from Tracker recompute function:
meteor.js?hash=27829e9…:930 Error: {{#each}} currently only accepts arrays, cursors or falsey values.
at badSequenceError (observe-sequence.js?hash=14ae414…:174)
at observe-sequence.js?hash=14ae414…:139
at Object.Tracker.nonreactive (tracker.js?hash=9f8a0ce…:631)
at observe-sequence.js?hash=14ae414…:116
at Tracker.Computation._compute (tracker.js?hash=9f8a0ce…:339)
at new Tracker.Computation (tracker.js?hash=9f8a0ce…:229)
at Object.Tracker.autorun (tracker.js?hash=9f8a0ce…:604)
at Object.observe (observe-sequence.js?hash=14ae414…:113)
at Blaze.View.<anonymous> (blaze.js?hash=813922c…:2819)
at fireCallbacks (blaze.js?hash=813922c…:2013)
为什么每个部分都失败了?我以为那些物品是一个阵列...