【问题标题】:Correct normalizer syntax for normalizing JSON用于规范化 JSON 的正确规范化器语法
【发布时间】:2018-01-02 12:03:08
【问题描述】:

我正在尝试使用 normalizer 来规范化一些 JSON。我的 JSON 看起来像

  total: 8029,
  items: [
    {
      id: 1,
      name: 'Jacket1',
      sku: '123',
      upc: '1',
      price: '99.99',
      images: ['url1', 'url2'],
      category: 'clothing',
      thumbnail:
        'https://cdn.zeplin.io/5969021e44c5978909d5278b/assets/1CE5FF07-E70F-4413-85BF-49C08AA559DE.png',
    }, ...

从示例中,我认为这可能有效

  const itemSchema = new schema.Entity('items')
  const itemsSchema = new schema.Entity('result', {
    items: [itemSchema],
  })

  const foo = normalize(fakeDatabase, itemsSchema)

但我最终得到了一个未定义的结果,并且该未定义的值包含一些时髦的东西。

我做错了什么?

【问题讨论】:

  • 我不认为itemsSchema 是必要的。试试normalize(fakeDatabase, { items: [itemSchema] })
  • 感谢您的回复。不幸的是,它没有用。我的 items 数组作为数字数组返回。我想我会用 lodash 解决这个问题
  • @OrB 您的回答实际上是正确的。对于那个很抱歉。如果您添加该答案,我会将其标记为正确的。

标签: json redux react-redux normalizr


【解决方案1】:

我不认为itemsSchema 是必要的。尝试任一:

normalize(fakeDatabase, { items: new schema.Array(itemSchema) })

normalize(fakeDatabase, { items: [itemSchema] })

【讨论】:

    猜你喜欢
    • 2012-05-22
    • 2022-09-28
    • 2015-03-24
    • 1970-01-01
    • 2016-02-27
    • 2013-12-25
    • 2013-08-21
    • 2016-05-13
    相关资源
    最近更新 更多