这是代码app / routes / profile.js
import Ember from 'ember';
import AuthenticatedRouteMixin from 'simple-auth/mixins/authenticated-route-mixin';
export default Ember.Route.extend(AuthenticatedRouteMixin, {
model: function() {
var _this = this;
return Ember.$.get('/user/profile').then(function(data)
_this.store.push('profile', data);
});
}
});
应用程序/模型/ profile.js
import DS from "ember-data";
export default DS.Model.extend({
name: DS.belongsTo('name')
});
应用程序/模型/ name.js
import DS from "ember-data";
export default DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string')
});
应用程序/串行器/ profile.js
import DS from "ember-data";
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
name: {embedded: 'always'}
}
});
我收到错误无法读取属性' typeKey'未定义的
这是来自服务器的JSON响应。
{
"id":"1",
"name":{
"id":"1",
"firstName":"first name",
"lastName":"last name"
}
}
答案 0 :(得分:1)
您需要服务器返回包装在类型对象中的有效负载:
{
"profiles": [{
"id":"1",
"name":{
"id":"1",
"firstName":"first name",
"lastName":"last name"
}
}]
}