Richard Knop
Richard Knop

Reputation: 83705

Ember: Assertion failed: The value that #each loops over must be an Array. You passed (generated home controller)

I am getting this error in the console:

Assertion failed: The value that #each loops over must be an Array. You passed (generated home controller)
Uncaught TypeError: Object [object Object] has no method 'addArrayObserver'

My HTML looks like this:

<script type="text/x-handlebars">
    <div id="top-panel">
        <h1>{{{title}}}</h1>
    </div>
    <div id="wrapper">
        <div id="content">
            {{outlet}}
        </div>
    </div>
</script>

<script type="text/x-handlebars" id="home">
    <table>
        <thead>
        <tr>
            <th>Id</th>
            <th>Foo</th>
            <th>Bar</th>
            <th>Foo Bar</th>
        </tr>
        </thead>

        <tbody>
        {{#each}}
        <tr>
            <td>{{itemId}}</td>
            <td>foo</td>
            <td>foo</td>
            <td>foo</td>
        </tr>
        {{/each}}
        </tbody>
    </table>
</script>

I have defined the home route like this:

App.Router.map(function () {
    this.resource("home", { path: "/" });
});

App.HomeRoute = Ember.Route.extend({
    model: function () {
        $.getJSON("mocks/items.json", function (items) {
            console.log(items);
            return items;
        });
    }
});

The console.log(items) logs an array of objects in the console so that is correct. I don't know why the each loop in the home template is not working then.

Upvotes: 3

Views: 4205

Answers (1)

Richard Knop
Richard Knop

Reputation: 83705

Found out where the problem is. I forgot to return the promise object inside the route:

App.HomeRoute = Ember.Route.extend({
    model: function () {
        $.getJSON("mocks/items.json", function (items) {
            console.log(items);
            return items;
        });
    }
});

Should be:

App.HomeRoute = Ember.Route.extend({
    model: function () {
        return $.getJSON("mocks/items.json", function (items) {
            console.log(items);
            return items;
        });
    }
});

Upvotes: 2

Related Questions