Redux 无法读取 Schema Normalizr

Redux Cannot read Schema Normalizr

我总是得到这样的错误:

Uncaught (in promise) TypeError: Cannot read property 'periodListSchema' of undefined

这是我的代码:

我的架构

import { schema, arrayOf } from 'normalizr';

export const periodSchema = new schema.Entity('activePeriod');
export const periodListSchema = new schema.Array(periodSchema);

我的规范化操作

then(response => {
            console.log('normalized response', normalize(response.schema.periodListSchema));

这是我的回复

{"activePeriod":[{"periodID":2,"periodName":"Periode 27","startDate":"2016-11-11","endDate":"2016-12-11","remark":"Periode Alpha 27","isActive":"1"}],"status":"OK"}

我的 Normalzr 库是 v3.2.2 有人可以帮我找出问题所在吗?我正在尝试理解这一点。

1)Uncaught (in promise) TypeError: Cannot read property 'periodListSchema' of undefined因为response没有schema属性所以报错periodListSchema属性 来自 undefined

2) 要规范化响应,您应该将句点数组传递给 normalize func,并指定 schema。此外,如果您有非标准名称 id 属性,那么您应该通过 idAttribute.

schema.Entity 构造函数的选项中指定名称

DEMO webpackbin

例子

import { schema, normalize } from 'normalizr';

export const periodSchema = new schema.Entity(
  'activePeriods',
  {},
  { idAttribute:'periodID' }
);
export const periodListSchema = [periodSchema];

const response = {"activePeriod":[{"periodID":2,"periodName":"Periode 27","startDate":"2016-11-11","endDate":"2016-12-11","remark":"Periode Alpha 27","isActive":"1"}],"status":"OK"};

console.log(
  normalize(response.activePeriod, periodListSchema)
);

结果

{
  "entities": {
    "activePeriods": {
      "2": {
        "periodID": 2,
        "periodName": "Periode 27",
        "startDate": "2016-11-11",
        "endDate": "2016-12-11",
        "remark": "Periode Alpha 27",
        "isActive": "1"
      }
    }
  },
  "result": [
    2
  ]
}