francesco.venica
francesco.venica

Reputation: 1721

display collection items - backbone

I have a problem trying to display collection items, in particular I get an error on collection.each() function:

getTodo: function(){
  this.todos = new TodosCollection;
  this.todos.query = new Parse.Query("test");
  this.todos.fetch({
success:function(obj){
    console.log(obj.length);
    obj.each(this.addOne);
    }
  });
},
addOne: function(todo){
  console.log(todo);
}

with this code I get this error:

Uncaught TypeError: undefined is not a function

console.log(obj.lenght) is != 0, so where is the error?

Upvotes: 1

Views: 60

Answers (1)

cbrandolino
cbrandolino

Reputation: 5883

The this from which you call the addOne method is the callback, not your object - and the callback's property addOne is undefined, so you're trying to call undefined as a function. You can bind the callback to the object scope this way:

success: _.bind(function(obj){
  obj.each(this.addOne);
}, this)

Upvotes: 1

Related Questions