我有两个模型 employee 和 empdetails
//employee model
import DS from 'ember-data';
export default DS.Model.extend({
empId : DS.attr(),
password : DS.attr(),
email : DS.attr(),
empdetails : DS.belongsTo("empdetails")
});
//empdetails model
import DS from 'ember-data';
export default DS.Model.extend({
firstName : DS.attr(),
lastName : DS.attr(),
dateOfJoining: DS.attr(),
employee : DS.belongsTo("employee")
});
我使用 RESTAdapter 进行 REST 调用。
//serializer
import DS from 'ember-data';
export default DS.JSONSerializer.extend({
});
当我尝试为员工发出获取请求时,出现以下错误
Please check your serializer and make sure it is serializing the relationship payload into a JSON API format. Error: Assertion Failed: Encountered a relationship identifier without a type for the belongsTo relationship 'empdetails' on , expected a json-api identifier with type 'empdetails' but found '{"id":"1","firstName":"xxx"}
我从我的后端得到下面的 JSON
[
{
"id": 1,
"email": "xyz@gmail.com",
"password": "12345678",
"empdetails": {
"id": 1,
"firstName": "xxx",
"lastName": "yyy",
"dateOfJoining": "22-10-2018"
}
}
]
谁能指导我改正错误
最佳答案
尝试按如下方式更新您的员工序列化程序:
import DS from 'ember-data';
export default DS.JSONSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
empdetails: {
serialize: 'records',
deserialize: 'records'
}
}});
参见 this article了解更多详情。
关于ember.js - Ember js属于关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52927111/