【问题标题】:Denormalize fails when trying to use idAttribute to generate id尝试使用 idAttribute 生成 id 时,非规范化失败
【发布时间】:2017-12-10 13:50:35
【问题描述】:

我正在使用以下架构:

import { schema } from 'normalizr';
import { guid } from '../utils/guid';

const track = new schema.Entity('audioTracks');

const audioClip = new schema.Entity('audioclips', {
  audioclips: [track],
  }, {
   idAttribute: (entity, parent) => `${parent.id}:${entity.id}`,
});

const segment = new schema.Entity('segments', {
    audioclips: [audioClip],
  }
);

const program = new schema.Entity('programs', {
  segments: [segment],
});

const json = new schema.Entity('jsons', {
  programs: [program],
});

export const portalAttributes = new schema.Entity('portalAttributes');

export const experience = new schema.Entity('experiences', {
  json,
  portalAttributes,
});

我的数据在音频剪辑部分使用相同的 ID,我不想合并。因为它们最终将包含唯一的数据。 规范化部分按预期工作,但是当我尝试将数据反规范化时失败并出现以下错误:

TypeError: Cannot read property 'id' of undefined
    at EntitySchema.idAttribute (http://localhost:4040/main.bundle.js:3782:59)
    at EntitySchema.getId (http://localhost:4040/vendor.bundle.js:70706:19)
    at unvisitEntity (http://localhost:4040/vendor.bundle.js:4787:19)
    at unvisit (http://localhost:4040/vendor.bundle.js:4821:14)
    at http://localhost:4040/vendor.bundle.js:70589:12
    at Array.map (native)
    at denormalize (http://localhost:4040/vendor.bundle.js:70588:37)

请帮助反规范化数据。

我在这个阶段所做的只是:

const normalized = normalize(payload, norm.experience);
const denorm = denormalize(normalized.result, norm.experience, normalized.entities)

我也可以按需提供输入数据,有点大。

【问题讨论】:

    标签: normalizr


    【解决方案1】:

    目前有一个拉取请求来解决我也遇到的这个精确问题。我建议您对此发表评论,您也希望此解决方案可以合并! https://github.com/paularmstrong/normalizr/pull/294

    【讨论】:

      猜你喜欢
      • 2012-11-16
      • 2020-05-21
      • 2015-12-15
      • 2018-01-15
      • 1970-01-01
      • 2015-02-16
      • 2020-12-20
      • 2013-08-21
      • 2016-05-13
      相关资源
      最近更新 更多