我总是得到这样的错误:
未捕获(承诺)TypeError:无法读取属性 未定义的'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 有人可以帮我找出什么是错的吗?我正试图理解这一点。
答案 0 :(得分:3)
1)Uncaught (in promise) TypeError: Cannot read property 'periodListSchema' of undefined
由于response
没有schema
属性,因此引发了此错误,因此您无法从{{1}获取periodListSchema
属性}
2)要规范化响应,您应该将句点数组传递给undefined
func,并指定normalize
。此外,如果您有非标准名称的id属性,则应在schema
构造函数的选项中通过schema.Entity
指定名称。
示例强>
idAttribute
<强>结果强>
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)
);