Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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')
})
like image 889
romainberger Avatar asked Aug 27 '26 05:08

romainberger


1 Answers

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')
})
like image 181
romainberger Avatar answered Aug 31 '26 22:08

romainberger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!