Redux的对象数组的Normalizr模式

时间:2018-04-18 23:10:21

标签: reactjs redux normalization normalizr

我的api当前回复如下:

[
   {  
      "device_id": "1234",
      "network_status": "Offline",
      "status": "Yes",
      "frequency": 50,

   },
   {  
      "device_id": "12345",
      "network_status": "online",
      "status": "no",
      "frequency": 123,

   },
   {  
      "device_id": "12346",
      "network_status": "online",
      "status": "no",
      "frequency": 423,

   },
]

使用最新文档: https://github.com/paularmstrong/normalizr/blob/master/docs/api.md#arraydefinition-schemaattribute

我知道这些文档已经更新,所以在堆栈上查看其他问题我还没有找到类似的例子。 ' responseData'包含来自api的响应,它是一个对象数组。 [{},{},...]

我的代码是

import { schema } from 'normalizr';

const deviceid = new schema.Entity('device_id');
const arrayOfDevices = new schema.Array({
  device_id : deviceid,
})

normalize(responseData, arrayOfDevices)

我想得到以下输出。具有device_id作为每个对象的键的实体对象。

  {
  entities: {
    device_id: { 
      '1234' : {
        .....
      },
      '12345' : {
        .....
      } ,
      '123456' : {
        .....
      }
    }
  },
  result: [
    ['1234','12345','123456'],
  ]
}

然而,我似乎只是得到了一个空实体以及其他所有内容被推送到结果中的以下响应

 {
  entities: {},
  result: [
   {  
      "device_id": "1234",
      "network_status": "Offline",
      "status": "Yes",
      "frequency": 50,

   },
   {  
      "device_id": "12345",
      "network_status": "online",
      "status": "no",
      "frequency": 123,

   },
   {  
      "device_id": "12346",
      "network_status": "online",
      "status": "no",
      "frequency": 423,

   },
  ]
}

我的normalizr代码似乎缺少什么?

1 个答案:

答案 0 :(得分:1)

Normalizr希望您的实体拥有id道具。如果包含id的字段具有其他名称,则必须明确定义它:

import { normalize, schema } from 'normalizr';

const data = [
  {
    "device_id": "1234",
    "network_status": "Offline",
    "status": "Yes",
    "frequency": 50,

  },
  {
    "device_id": "12345",
    "network_status": "online",
    "status": "no",
    "frequency": 123,

  },
  {
    "device_id": "12346",
    "network_status": "online",
    "status": "no",
    "frequency": 423,

  },
];

const device = new schema.Entity('devices', {}, { idAttribute: 'device_id' });

const normalizedData = normalize(data, [device]);

console.log(normalized);

<强>输出:

{
    "entities": {
        "devices": {
            "1234": {
                "device_id": "1234",
                "network_status": "Offline",
                "status": "Yes",
                "frequency": 50
            },
            "12345": {
                "device_id": "12345",
                "network_status": "online",
                "status": "no",
                "frequency": 123
            },
            "12346": {
                "device_id": "12346",
                "network_status": "online",
                "status": "no",
                "frequency": 423
            }
        }
    },
    "result": [
        "1234",
        "12345",
        "12346"
    ]
}

Edit w0z95yy8zl

还要与normalizr docs中的this example进行比较。

我建议不要将id字段命名为device_id,因为如果您正确命名保存实体的变量,这通常是多余的。如果你是拥有一系列设备,您应该将其命名为devices。然后很明显,里面对象的id字段是设备ID。无需在属性名称中重复此操作。