1

I'm having a problem trying to implement the backbone relational within our application.

I get this error after i try to fetch a RelationalModel: Uncaught TypeError: Cannot read property 'id' of null

This is the module

App.Models.Story = Backbone.RelationalModel.extend({
urlRoot: '/rest/v1/story/',
url:function(){
     if (this.isNew()) {
          return this.urlRoot;
    } else {
     return  this.urlRoot + this.id + "/";
    } 
},
idAttribute: 'id',
relations: [{
      type: Backbone.HasMany,
      key: 'pieces',
      createModels: true,
      relatedModel: 'App.Models.Piece',
      collectionType: 'App.Collections.Pieces',
      includeInJSON: false,
      reverseRelation: {
         key: 'story'
     }
  }],
     initialize: function(){
         this.get('pieces').url = _.bind(function(){
             return this.url() + "piece/"
         }, this);
        console.log("hello from story model");
    }
});

This is the collection

 App.Collections.Stories = Backbone.Collection.extend({
 url:'/rest/v1/story/',
 model: App.Models.Story
});

I retrieve data from my API like this

get all stories    : GET /rest/v1/story/
get story no' 10   : GET /rest/v1/story/10/
get story pieces   : GET /rest/v1/story/10/piece/
get piece no' 612  : GET /rest/v1/piece/612/

Now when i try to do

var story = App.Models.Story({id:10});
story.fetch()

I get this error: Uncaught TypeError: Cannot read property 'id' of null

The Object itself created successfully trough the responded Json.

The error breaks the RelationalModel from setting up the relations.

Thanks.

Community
  • 1
  • 1
  • 1
    Could you show the JSON data that you receive from `story.fetch()`? I had a similar case, and the problem was caused by `null`s received from server. – mik01aj Mar 04 '13 at 16:38

0 Answers0