我正在尝试通过RESTAdapter获取数据(遵循教程),我收到一个我不理解的错误。
"Error while processing route: players.index" "factory is undefined" "Store<.modelFor@http://localhost:4200/assets/vendor.js:74981:1
任何人都知道我缺少什么?我需要安装插件吗?
路由/播放器/ index.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.store.find('player');
}
});
模型/ player.js
import DS from 'ember-data';
export default DS.Model.extend({
finalScore: DS.attr('number'),
name: DS.attr('string'),
heightFormatted: DS.attr('string'),
heightCm: DS.attr('number'),
weightLb: DS.attr('number'),
weightKg: DS.attr('string'),
birthplace: DS.attr('string'),
birthdate: DS.attr('string'),
neoId: DS.attr('number'),
position: DS.attr('string'),
number: DS.attr('string'),
faults: DS.attr('number'),
stats: DS.belongsTo('stat')
});
模型/ stat.js
import DS from 'ember-data';
export default DS.Model.extend({
points: DS.attr('number'),
assits: DS.attr('number'),
defr: DS.attr('number'),
ofr: DS.attr('number'),
steals: DS.attr('number'),
blocks: DS.attr('number'),
turnovers: DS.attr('number'),
fta: DS.attr('number'),
ftm: DS.attr('number'),
fga: DS.attr('number'),
fgm: DS.attr('number'),
lsa: DS.attr('number'),
lsm: DS.attr('number'),
minutes: DS.attr('number'),
isStarter: DS.attr('boolean'),
gamesPlayed: DS.attr('number'),
player: DS.belongsTo('player')
});
适配器/ application.js中
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
host: 'http://localhost:3000',
namespace: 'api'
});
JSON
{
"players": [
{
"neo_id": 3264,
"name": "Jeff Green",
"height_cm": 205,
"height_formatted": "6'9\"",
"weight_lb": 235,
"weight_kg": "106.8",
"position": "PF",
"number": "8",
"birthplace": "Cheverly, Maryland, USA",
"birthdate": "523843200",
"stats": {
"final_score": 344,
"games_played": 16,
"minutes": 34.5,
"points": 19,
"assits": 1.75,
"defr": 3.75,
"ofr": 1,
"steals": 0.94,
"blocks": 0.69,
"turnovers": 1.56,
"faults": 1.94,
"neo_id": 3264
}
},
{
"neo_id": 3265,
"name": "Erik Murphy",
"height_cm": 208,
"height_formatted": "6'10\"",
"weight_lb": 230,
"weight_kg": "104.6",
"position": "PF",
"number": "-1",
"birthplace": "Lyon, France",
"birthdate": "656899200",
"stats": {}
}
]
}
答案 0 :(得分:2)
我无法重现您的确切错误,但使用您分享的内容我可以通过进行这些更改来使其工作。如果您的代码有不同之处,请留下评论,我会看一下。
<强>串行器/ application.js中强>
由于您使用的是自定义主键(neo_id而不是id),因此您可以在此更改此设置,并指出stats
数据中嵌入了players
条记录:
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs : {
stats: { embedded: 'always' }
},
primaryKey : 'neo_id'
});