EmberJS hasMany 不获取关系

EmberJS hasMany doesn't fetch relationships

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

{
"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 负载与 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'
});