2
votes

I'm getting this error when trying to consume the json from the backend. I'm using Ember CLI version 2.5.0 and the RestAdapter.

Here's my routes/products/index.js looks like:

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

And here's what my json looks like:

{
   "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
      }
   }
}

And by request, here's the model:

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
Can you post your model?Marek Grác
Can you post your adapter, serializer (if any) and stack trace from console, which line is error coming from?Daniel Kmak

2 Answers

5
votes

I was having the same issue. This worked for me:

//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'}
  }
});
3
votes

You have the brand: belongsTo('brand')

but you forgot the group: belongsTo('group')

also .. these two ( brand, group ) have to be declared as embedded with the DS.EmbeddedRecordsMixin in the product serializer if you are going to embed them this way