【问题标题】:Meteor throws an error when I try output array in template with {{#each}} loop当我尝试使用 {{#each}} 循环在模板中输出数组时,Meteor 引发错误
【发布时间】:2016-04-15 02:28:39
【问题描述】:

我正在学习 Meter,需要一些帮助。我使用 Collection2 和 Autoform 创建了一个表单。

现在我尝试在列表中输出提交的数据,但我坚持这一步。

这是我的模板,我尝试在其中输出数据:

<template name="Transactions">
    {{> NewTransaction}}
    <section class="recipes">
        {{#each transactions}}
            <article>
                <h3>{{title}}</h3>
                <p>{{desc}}</p>
                <p>
                    {{ #each category}}
                        <span class="ingredient">{{name}}</span>
                    {{ /each}}
                </p>
            </article>
        {{/each}}
    </section>
</template>

这是我的收藏:

Category = new SimpleSchema({
    name: {
        type: String
    }
});

TransactionSchema = new SimpleSchema({

    title: {
        type: String,
        label: "Title"
    },
    price: {
        type: Number,
        label: "Price"
    },
    category: {
        type: [Category]
    },
    desc: {
        type: String,
        label: "Description"
    },
    author: {
        type: String,
        label: "Author",
        autoValue: function() {
            return this.userId
        },
        autoform: {
            type: "hidden"
        }
    },
    createdAt: {
        type: Date,
        label: "Created At",
        autoValue: function() {
            return new Date()
        },
        autoform: {
            type: "hidden"
        }
    }

});

如果我删除输出类别的内部每个循环,一切正常。但是如果我将它返回 Meteor 会抛出一个错误:{{#each}} 目前只接受数组、游标或错误值。

我认为这是一个简单的问题,解决方案很容易,但我找不到它,因为我是 Meteor 的新手。

我将不胜感激!

【问题讨论】:

  • 很遗憾无法正常工作。
  • 试着简单地输出 {{category}} 来看看你得到了什么......它“应该”是每个你的模式的数组,但如果它为空怎么办?也许这就是引发错误的原因?
  • 返回[object Object],[object Object]
  • 令人困惑...我希望[对象对象,对象对象]。你写一个返回 this.category 的帮助器,看看调试器中有什么?
  • 它返回未定义

标签: meteor meteor-blaze meteor-collection2


【解决方案1】:

快速提问,您是否在代码中定义“事务”助手?例如,

Template.body.helpers({
    tasks: [
        { text: "This is task 1" },
        { text: "This is task 2" },
        { text: "This is task 3" }
    ]
});

现在我已经定义了任务,我可以遍历任务

{{#each tasks}}
  {{text}}
{{/each}}

【讨论】:

  • 是的,这里:Template.Transactions.helpers({ transactions: ()=> { return Transactions.find({}); } });
  • 如果您尝试调用 {{category[0]}} 或 {{category[0].name}} 会怎样
猜你喜欢
  • 1970-01-01
  • 2014-02-23
  • 2022-01-01
  • 1970-01-01
  • 2017-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-15
相关资源
最近更新 更多