处理路由时出错:...断言失败:您尝试推送具有某种类型的数据...但找不到具有该名称的模型
Error while processing route: ... Assertion Failed: You tried to push data with a type ... but no model could be found with that name
我在使用 relationships/includes 和使用 Ember 和 JSONAPI 的序列化器时遇到了一些问题。
我有一个显示患者信息的路径:
import Ember from 'ember';
export default Ember.Route.extend({
model(params){
return this.store.find('patient', params.patient_id);
}
});
我有这两个模型:
patient
:
import DS from 'ember-data';
export default DS.Model.extend(validations, {
name : DS.attr('string'),
email : DS.attr('string'),
gender : DS.attr('string'),
birthDate : DS.attr('date'),
description : DS.attr('string'),
anamnesis : DS.belongsTo('anamnesis')
});
和anamnesis
:
import DS from 'ember-data';
export default DS.Model.extend({
smokes : DS.attr('boolean'),
drinks : DS.attr('boolean'),
drugUser : DS.attr('boolean')
});
如你所见,我和他们之间是一对一的关系。
现在,我的 Ruby 在 Rails 后端
上收到了这个 JSONAPI 响应
{
"data":{
"id":"3",
"type":"patients",
"attributes":{
"name":"paciente 3",
"gender":"female",
"email":"teste3@teste.com",
"birth-date":"2017-06-04T02:59:37.435Z"
},
"relationships":{
"anamnesis":{
"data":{
"id":"2",
"type":"anamneses"
}
}
}
},
"included":[
{
"id":"2",
"type":"anamneses",
"attributes":{
"smokes":null,
"drinks":null,
"drug-user":null
}
}
]
}
当尝试序列化 relationship
和 included
时,它会查找 anamnese
(来自后端的响应类型 anamneses
)模型而不是 anamnesis
.
Error while processing route: ... Cannot read property 'type' of null TypeError: Cannot read property 'type' of null
所以我不得不创建一个 PatientSerializer
来解决这个问题:
import DS from 'ember-data';
import Ember from 'ember';
export default DS.JSONAPISerializer.extend({
modelNameFromPayloadKey(payloadKey){
if(payloadKey === 'anamneses'){
return 'anamnesis';
} else {
return this._super(payloadKey);
}
},
modelNameFromPayloadType(payloadType){
if(payloadKey === 'anamneses'){
return 'anamnesis';
} else {
return this._super(payloadType);
}
},
});
我知道这可能不是最好的解决方案,但我什至无法尝试其他方法,因为它现在向我显示此错误:
Error while processing route: ... Assertion Failed: You tried to push data with a type 'anamnese' but no model could be found with that name.
我错过了什么?我修错方法了吗?
- 将
anamnesis : DS.belongsTo('anamnesis')
更改为 anamnesis : DS.belongsTo('anamnese')
。
- 将模型文件名
anamnesis
更改为 anamnese
。
- 您不需要引入
Patient
序列化程序来更改模型名称。
以上更改将起作用。看看 this twiddle
PS:find
在最新版本中已弃用,请考虑 findAll
我在使用 relationships/includes 和使用 Ember 和 JSONAPI 的序列化器时遇到了一些问题。
我有一个显示患者信息的路径:
import Ember from 'ember';
export default Ember.Route.extend({
model(params){
return this.store.find('patient', params.patient_id);
}
});
我有这两个模型:
patient
:
import DS from 'ember-data';
export default DS.Model.extend(validations, {
name : DS.attr('string'),
email : DS.attr('string'),
gender : DS.attr('string'),
birthDate : DS.attr('date'),
description : DS.attr('string'),
anamnesis : DS.belongsTo('anamnesis')
});
和anamnesis
:
import DS from 'ember-data';
export default DS.Model.extend({
smokes : DS.attr('boolean'),
drinks : DS.attr('boolean'),
drugUser : DS.attr('boolean')
});
如你所见,我和他们之间是一对一的关系。
现在,我的 Ruby 在 Rails 后端
上收到了这个 JSONAPI 响应{
"data":{
"id":"3",
"type":"patients",
"attributes":{
"name":"paciente 3",
"gender":"female",
"email":"teste3@teste.com",
"birth-date":"2017-06-04T02:59:37.435Z"
},
"relationships":{
"anamnesis":{
"data":{
"id":"2",
"type":"anamneses"
}
}
}
},
"included":[
{
"id":"2",
"type":"anamneses",
"attributes":{
"smokes":null,
"drinks":null,
"drug-user":null
}
}
]
}
当尝试序列化 relationship
和 included
时,它会查找 anamnese
(来自后端的响应类型 anamneses
)模型而不是 anamnesis
.
Error while processing route: ... Cannot read property 'type' of null TypeError: Cannot read property 'type' of null
所以我不得不创建一个 PatientSerializer
来解决这个问题:
import DS from 'ember-data';
import Ember from 'ember';
export default DS.JSONAPISerializer.extend({
modelNameFromPayloadKey(payloadKey){
if(payloadKey === 'anamneses'){
return 'anamnesis';
} else {
return this._super(payloadKey);
}
},
modelNameFromPayloadType(payloadType){
if(payloadKey === 'anamneses'){
return 'anamnesis';
} else {
return this._super(payloadType);
}
},
});
我知道这可能不是最好的解决方案,但我什至无法尝试其他方法,因为它现在向我显示此错误:
Error while processing route: ... Assertion Failed: You tried to push data with a type 'anamnese' but no model could be found with that name.
我错过了什么?我修错方法了吗?
- 将
anamnesis : DS.belongsTo('anamnesis')
更改为anamnesis : DS.belongsTo('anamnese')
。 - 将模型文件名
anamnesis
更改为anamnese
。 - 您不需要引入
Patient
序列化程序来更改模型名称。
以上更改将起作用。看看 this twiddle
PS:find
在最新版本中已弃用,请考虑 findAll