reactjs - Redux 无法读取 Schema Normalizr

标签 reactjs redux 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) 未捕获( promise 中)类型错误:无法读取未定义的属性“periodListSchema” 由于response没有schema已引发此错误code> 属性,因此您无法从 undefined

获取 periodListSchema 属性

2) 要标准化响应,您应该将句点数组传递给 normalize 函数,并指定schema。另外,如果您有 id 属性的非标准名称,那么您应该通过 idAttributeschema.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
  ]
}

关于reactjs - Redux 无法读取 Schema Normalizr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43307068/

相关文章:

javascript - 带有 "this"关键字的 React 类行为

javascript - Redux 工具包 createAsyncThunk 问题 : state updates after async dispatch call?

javascript - 在 Normalizr 模式中设置深度关联

redux - 如何将更新合并到标准化的Redux状态

redux - 如何使用 Normalizr 处理基本的嵌套 JSON?

javascript - 跨并行组件 react 传递状态

javascript - 是否可以只显示 SVG 图标的一半?

javascript - 当元素位于视口(viewport)顶部时如何开始更新滚动百分比

reactjs - 如何在 ReactDOM.render 方法中渲染 Redux 容器

javascript - 使用 ReactJS + Redux 时是否需要 componentWillReceiveProps 和 ReplaceProps?