Ember-data错误:"断言失败:您需要将模型名称传递给商店的modelFor方法"

时间:2016-06-03 17:46:10

标签: ember.js ember-data

我在尝试从后端使用json时遇到此错误。 我使用的是Ember CLI 2.5.0版和RestAdapter。

这里的路线/ products / index.js如下:

export default Ember.Route.extend({
  actions: {
   [...]
  },
  model: function() {
    return this.store.findAll('product');
  }
});

这就是我的json的样子:

{
   "products":[
      {
         "id":9,
         "name":"Product A",
         "price_cents":1500,
         "margin_cents":0,
         "commission":0,
         "expiration":null,
         "track_stock":false,
         "stock_amount":5,
         "brand":{
            "id":2,
            "name":"SuperPet"
         },
         "group":{
            "id":1,
            "name":"Group A"
         }
      },
      {
         "id":8,
         "name":"Product B",
         "price_cents":1500,
         "margin_cents":0,
         "commission":0,
         "expiration":null,
         "track_stock":false,
         "stock_amount":5,
         "brand":{
            "id":1,
            "name":"Whiskas"
         },
         "group":{
            "id":1,
            "name":"Group B"
         }
      }
   ],
   "meta":{
      "pagination":{
         "per_page":null,
         "total_pages":4,
         "total_objects":10
      }
   }
}

根据要求,这是模型:

import DS from 'ember-data';

const { attr, belongsTo } = DS;

export default DS.Model.extend({
  name: attr('string'),
  priceCents: attr('number'),
  marginCents: attr('number'),
  comission: attr('number'),
  expiration: attr('date'),
  trackStock: attr('boolean'),
  stockAmount: attr('number'),

  brand: belongsTo('brand')

});

2 个答案:

答案 0 :(得分:5)

我遇到了同样的问题。这对我有用:

//app/serializers/product.js
import DS from 'ember-data';
import Ember from 'ember';

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

答案 1 :(得分:3)

您拥有品牌:belongsTo('品牌')

但你忘了这个组:belongsTo('group')

同样..如果要以这种方式嵌入它们,必须在产品序列化器中将这两个(品牌,组)声明为嵌入DS.EmbeddedRecordsMixin