Stefano Maglione
Stefano Maglione

Reputation: 4160

Error in render backbone by handlebars:Uncaught TypeError: Cannot read property 'Object' of undefined

I have fetched by collection and used function toJson() and the result is this:

Object {results: Array[4]};

So I am passing this object to handlebars but compare the error:

Uncaught TypeError: Cannot read property 'Object' of undefined.

var wrapper;
var HomeView = Backbone.View.extend({

    template: Handlebars.compile(template),

    events: {

    },

    initialize: function () {

        console.log("inhomeview");

        var amici = new Usercollection();
        amici.fetch({
            success: function () {
                amici.each(function (object) {

                    console.log(object.toJSON());
                    wrapper = object.toJSON();

                });
            },
            error: function (amici, error) {
                // The collection could not be retrieved.
            }
        });

        this.render();

    },

    render: function () {

        var context = wrapper;
        var html = this.template(context);
        console.log(html);

        $('#pagina').html(this.$el.html(html));

    }

});

return HomeView;

});

and the template is this:

<section id="home">
    <button>Scream</button>
    <input type="text" name="scream">
    <button>mt</button>
    <section class="lista">
        <ul>{{#each Object}}
            <li>
                <a href="#user/{{objectId}}">
                    <img src="{{avatar.url}}" width="69" height="69" />
                     <h3>        {{username}} {{email}}</h3> 
                     <h4>7 m</h4>
                    <h5>32 min</h5>
                </a>
            </li>{{/each}}</ul>
    </section>
</section>

Upvotes: 0

Views: 792

Answers (1)

TheJoe
TheJoe

Reputation: 256

Don't call render until the callback executes.

amici.fetch({
            success: function () {
                amici.each(function (object) {

                    console.log(object.toJSON());
                    wrapper = object.toJSON();
                    this.render();

                });
            },
            error: function (amici, error) {
                // The collection could not be retrieved.
            }
        });

Upvotes: 1

Related Questions