EmberJS hasMany没有获取关系

时间:2019-02-07 15:45:26

标签: ember.js ember-data

我在序列化响应时遇到问题,如下所示:

{
"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,但它没有帮助我。您能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

您的api有效负载与ember-data的默认JSONAPISerializer expects(没有type属性=>缺少类型错误)不匹配。
您发布了一个基于RESTSerializer的自定义序列化程序,但似乎放置在不合适的位置,因此ember-data仍使用默认的JSONAPISerializer。另外,最好使用JSONSerializer

由于您的有效负载的记录ID(uniqueIdpointOfSaleUniqueId)具有不同的属性,因此您必须为每个模型创建自定义序列化程序,以设置不同的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'
});