2017-04-09 35 views
1

我總是得到錯誤是這樣的:終極版無法讀取架構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); 

我正常化行動

​​

這是我的迴應

{"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 有人可以幫我找出什麼是錯的?我試圖理解這一點。

回答

3

1)Uncaught (in promise) TypeError: Cannot read property 'periodListSchema' of undefined這個錯誤已經被拋出,因爲response沒有schema屬性,因此,你不能從undefined

2)periodListSchema屬性正常化的反應,你應該通過時間的數組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 
    ] 
} 
+0

我得到這個錯誤:錯誤:給定的歸一化輸入意外。預期類型爲「對象」,找到「未定義」。 正常化 – ukiyakimura

+0

我已經解決了這個問題。忘記指定更多。我的response.data.activePeriod。無論如何 – ukiyakimura