用于 Redux 的对象数组的 Normalizr 模式

Normalizr schema for array of objects for Redux

我的 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 代码似乎缺少什么?

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"
    ]
}

同时与 normalizr 文档中的 this example 进行比较。

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