Sergii Korh
Sergii Korh

Reputation: 71

Meteor throws an error when I try output array in template with {{#each}} loop

I'm learning Meter and need some help. I created a form using Collection2 and Autoform.

Now I try to output submitted data in a list, but I stuck on this step.

Here is my Template, where I try to output the data:

<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>

Here is my collection:

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"
        }
    }

});

If I remove inner each loop which outputs the categories, everything works fine. But if I return it back Meteor throws an error: {{#each}} currently only accepts arrays, cursors or falsey values.

I think that it is simple problem and the solution is easy, but I can't find it, because I'm new to Meteor.

I would appreciate some help!

Upvotes: 2

Views: 93

Answers (1)

Dev Coffee
Dev Coffee

Reputation: 21

Quick question, are you defining the 'transactions' helper in your code? For example,

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

Now that I have tasks defined I can loop through the tasks with

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

Upvotes: 1

Related Questions