【问题标题】:Redux Cannot read Schema NormalizrRedux 无法读取 Schema Normalizr
【发布时间】:2017-09-04 13:07:35
【问题描述】:

我总是收到这样的错误:

未捕获(承诺中)类型错误:无法读取属性 'periodListSchema' 未定义

这是我的代码:

我的架构

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 有人可以帮我找出问题所在吗?我正在努力理解这一点。

【问题讨论】:

    标签: reactjs redux normalizr


    【解决方案1】:

    1) Uncaught (in promise) TypeError: Cannot read property 'periodListSchema' of undefined 抛出此错误是因为response 没有schema 属性,因此您无法从undefined 获取periodListSchema 属性

    2) 要标准化响应,您应该将句点数组传递给normalize func,并指定schema。另外,如果您有非标准的 id 属性名称,那么您应该在 schema.Entity 构造函数的选项中指定名称,通过 idAttribute

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

    【讨论】:

    • 我收到此错误:错误:为规范化提供了意外的输入。预期类型为“对象”,发现“未定义”。在正常化
    • 我已经解决了这个问题。忘记指定更多。我的 response.data.activePeriod。还是谢谢
    • @ukiyakimura 可以分享一下 dision 吗?
    猜你喜欢
    • 2020-11-29
    • 2017-01-23
    • 2016-10-08
    • 2012-10-11
    • 1970-01-01
    • 2016-11-03
    • 2019-02-11
    • 2022-11-20
    • 2021-12-01
    相关资源
    最近更新 更多