Search code examples
jsonreduxreact-reduxnormalizr

Correct normalizer syntax for normalizing JSON


I'm trying to use normalizer to normalize some JSON. My JSON looks like

  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',
    }, ...

and from the examples, I thought this might work

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

  const foo = normalize(fakeDatabase, itemsSchema)

But I end up with one result that is undefined, and that undefined value has some funky stuff in it.

enter image description here

What am I doing wrong?


Solution

  • I don't believe itemsSchema is necessary. Try either:

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

    or

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