我总是收到这样的错误:
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 属性的非标准名称,那么您应该通过 idAttribute
在 schema.Entity
构造函数的选项中指定名称。
示例
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/