romainberger
romainberger

Reputation: 4558

Ember data: Uncaught TypeError: Cannot call method 'modelFor' of undefined

I am trying to define my models with ember data but for some reason as soon as I add some hasMany or belongsTo relations, I get the error 'Uncaught TypeError: Cannot call method 'modelFor' of undefined'

What am I doing wrong?

App.User = DS.Model.extend({
  username: DS.attr('string'),
  facebook_id: DS.attr('string'),
  staff: DS.attr('boolean', {defaultValue: false}),
  createdAt: DS.attr('date'),
  posts: DS.hasMany('post', {async: true}),
  comments: DS.hasMany('comment', {async: true)
})

App.Post = DS.Model.extend({
  title: DS.attr('string'),
  image: DS.attr('string'),
  track: DS.attr('string'),
  createdAt: DS.attr('date'),
  user: DS.belongsTo('user'),
  comments: DS.hasMany('comment', {async: true})
})

App.Comment = DS.Model.extend({
  user: DS.belongsTo('user'),
  post: DS.belongsTo('post'),
  track: DS.attr('string'),
  createdAt: DS.attr('date')
})

Upvotes: 3

Views: 967

Answers (2)

calcsam
calcsam

Reputation: 301

Note also that you can get this error when you reference a model that does not exist, e.g.:

App.ItServiceUser = DS.Model.extend({
    companyService: DS.belongsTo('App.CompanyITService'),
    employee: DS.belongsTo('App.AllEmployee'),
    employeeID: attr('string'),
    isLeadAdmin: attr('string'),
    isAdmin: attr('boolean'),
    isEditing: attr('boolean'),
    username: attr('string'),
    firstName: attr('string'),
    lastName: attr('string'),
    userChoiceSet: attr('string')
})

Ran this code from console:

> user = App.ItServiceUser.find(2216)
g {id: "2216", store: g, _reference: Object, stateManager: (...), _changesToSync: Object…}
> user.set("lastName", "Anthony")
g {id: "2216", store: g, _reference: Object, stateManager: (...), _changesToSync: Object…}
> user.save()
q {_promiseCallbacks: Object, constructor: function, then: function, on: function, off: function…}
Uncaught TypeError: Cannot read property 'toString' of undefined ember-data.js:2540

Removed the line referencing a model that doesn't exist, and I was fine:

companyService: DS.belongsTo('App.CompanyITService'),

Upvotes: 0

romainberger
romainberger

Reputation: 4558

Solved it by specifying the app name in the relationships, e.g. instead of hasMany('comment') I use hasMany('App.Comment'). Not sure what's happening as the former is what is shown in the docs.

App.User = DS.Model.extend({
  username: DS.attr('string'),
  facebook_id: DS.attr('string'),
  staff: DS.attr('boolean', {defaultValue: false}),
  createdAt: DS.attr('date'),
  posts: DS.hasMany('App.Post', {async: true}),
  comments: DS.hasMany('App.Comment', {async: true)
})

App.Post = DS.Model.extend({
  title: DS.attr('string'),
  image: DS.attr('string'),
  track: DS.attr('string'),
  createdAt: DS.attr('date'),
  user: DS.belongsTo('App.User'),
  comments: DS.hasMany('App.Comment', {async: true})
})

App.Comment = DS.Model.extend({
  user: DS.belongsTo('App.User'),
  post: DS.belongsTo('App.Post'),
  track: DS.attr('string'),
  createdAt: DS.attr('date')
})

Upvotes: 1

Related Questions