Jay*_*Jay 2 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');
}
});
Run Code Online (Sandbox Code Playgroud)
这就是我的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
}
}
}
Run Code Online (Sandbox Code Playgroud)
根据要求,这是模型:
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')
});
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题.这对我有用:
//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'}
}
});
Run Code Online (Sandbox Code Playgroud)