我有我的序列化响应,看起来像一个问题:
{
"data": [
{
"id": 930,
"uniqueId": "0d3a04cb-231c-4998-b4d3-9436a0a3138e",
"name": "DRINKI",
"lastEditDate": "2018-02-12T13:30:32",
"lastEditDateUTC": "2018-02-12T12:30:32",
"deleted": false,
"discountable": true,
"productCategoryPointOfSales": []
},
{
"id": 921,
"uniqueId": "5fbf423a-4932-47ca-b32f-5d3612dd73ee",
"name": "BALOTYNKI SOLO",
"lastEditDate": "2019-02-07T14:20:15",
"lastEditDateUTC": "2019-02-07T13:20:15",
"deleted": false,
"label": "",
"color": "#a0a5a9",
"discountable": true,
"productCategoryPointOfSales": [
{
"id": 142,
"pointOfSaleUniqueId": "98e370f2-9d37-4473-9446-d82e442593fe",
"directionId": 54,
"directionUniqueId": "f0c986c0-ef85-4a46-86ea-cd997981fe8a",
"kitchenUniqueId": "f0c986c0-ef85-4a46-86ea-cd997981fe8a",
"inactive": false
}
]
}
],
"total": 0
}
和错误,我得到:
Encountered a relationship identifier without a type for the hasMany relationship 'productCategoryPointOfSales' on <category:5fbf423a-4932-47ca-b32f-5d3612dd73ee>, expected a json-api identifier with type 'product-category-point-of-sale' but found '{"id":"142","pointOfSaleUniqueId":"98e370f2-9d37-4473-9446-d82e442593fe","directionId":54,"directionUniqueId":"f0c986c0-ef85-4a46-86ea-cd997981fe8a","kitchenUniqueId":"f0c986c0-ef85-4a46-86ea-cd997981fe8a","inactive":false}'. Please check your serializer and make sure it is serializing the relationship payload into a JSON API format.
楷模:
export default DS.Model.extend({
productCategoryPointOfSales: DS.hasMany('product-category-point-of-sale'),
uniqueId: DS.attr('string'),
name: DS.attr('string'),
label: DS.attr('string'),
color: DS.attr('string'),
discountable: DS.attr('boolean')
});
export default DS.Model.extend({
category: DS.belongsTo('category'),
pointOfSaleUniqueId: DS.attr('string'),
directionId: DS.attr('string'),
directionUniqueId: DS.attr('string'),
kitchenUniqueId: DS.attr('string'),
inactive: DS.attr('boolean')
});
而我的串行:
export default DS.RESTSerializer.extend(EmbeddedRecordMixin, {
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
payload = {
category: payload.data,
};
return this._super(store, primaryModelClass, payload, id, requestType);
},
primaryKey: 'uniqueId',
attrs: {
productCategoryPointOfSales: {embedded: 'always'}
}
});
我很新的EmberJS和不知道如何解决这个问题。我跟着一些教程,并与EmbeddedRecordMixin尝试,但它并没有帮助我。能否请你帮我搞清楚了这一点?
您的API负载不匹配,什么烬数据的默认JSONAPISerializer expects(无type
属性=>缺少类型错误)。
您发布基于RESTSerializer自定义序列,但它似乎不是在正确的地方,所以烬数据仍然使用默认JSONAPISerializer。你也可能是一个更好的JSONSerializer。
当你的有效载荷有记录ID(uniqueId
and pointOfSaleUniqueId
)不同的属性,你必须创建每个模型的自定义序列化,设置不同的primaryKey
和嵌入式记录细节。
我创建了一个ember-twiddle example,它有三个串行。
应用程序序列化,默认:
// /app/serializers/application.js
import JSONSerializer from 'ember-data/serializers/json';
export default JSONSerializer.extend({
// use uniqueId as ember-data model id
primaryKey: 'uniqueId',
normalizeResponse(store, primaryModelClass, payload, id, requestType) {
// extract data from payload, so JSONSerializer finds the records
let normalizedPayload = payload.data;
// call the JSONSerializer.normalizeResponse with the extracted payload
return this._super(store, primaryModelClass, normalizedPayload, id, requestType);
}
});
对于category
模式,支持嵌入式productCategoryPointOfSales
,扩展了应用程序序列化,并添加EmbeddedRecordsMixin
:
// /app/serializers/category.js
import ApplicationSerializer from './application';
import EmbeddedRecordsMixin from 'ember-data/serializers/embedded-records-mixin';
export default ApplicationSerializer.extend(EmbeddedRecordsMixin, {
attrs: {
productCategoryPointOfSales: { embedded: 'always' }
}
});
对于productCategoryPointOfSale
,使用不同的primaryKey
:
// /app/serializers/product-category-point-of-sale.js
import ApplicationSerializer from './application';
export default ApplicationSerializer.extend({
primaryKey: 'pointOfSaleUniqueId'
});