Getting error: You need to pass a model name to the store's modelFor method

时间秒杀一切 提交于 2019-12-23 13:02:24

问题


Using Ember 2.6.0

Have the following routes defined:

Router.map(function() {
  this.route('brands');
  this.route('brand', {path: '/brands/:brand_id'});
});

The model for brand is:

export default Model.extend({
  name: attr('string'),
  description: attr('string'),
  dateCreated: attr('date'),
  lastUpdated: attr('date'),
  regions: hasMany('region')
});

And the model for region is:

export default Model.extend({
  name: attr('string'),
  dateCreated: attr('date'),
  lastUpdated: attr('date'),
  brand: belongsTo('brand')
});

Now, when trying to access /brands I am doing this in the route:

export default Ember.Route.extend({
  model() {
    return this.store.findAll('brand');
  }
});

I am getting the following error:

You need to pass a model name to the store's modelFor method

This worked prior to adding the brand route and region relationships. Brand is the parent, so I'm not sure why this isn't working.

UPDATE:

Removing regions: hasMany('region') from the brand model allows things to work again. Not sure why it isn't working with the defined relationship.


回答1:


So it turns out I had to create serializers for each object. For brand I have:

import RESTSerializer from 'ember-data/serializers/rest';
import DS from 'ember-data';

export default RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
      regions: {embedded: 'always'}
  }
});

And for region I have:

import RESTSerializer from 'ember-data/serializers/rest';

export default RESTSerializer.extend({
});

Technically, it works without the region serializer but I get a warning that ember couldn't find regions inside the region model. I may submit a ticket to ember to suggest not having to create an empty RESTSerializer just to avoid this warning.



来源:https://stackoverflow.com/questions/37927705/getting-error-you-need-to-pass-a-model-name-to-the-stores-modelfor-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!