我有一个反应原生应用程序,我在IOS模拟器中运行。在初始化期间,它会读取Parse数据库并使用以下reducer填充redux数据存储区:
export default function items( state = [], action ) {
switch (action.type) {
case types.ADD_ASSIGNMENT:
return [
...state,
{
assignmentId: action.assignmentId,
shopperId: action.shopperId,
itemId: action.itemId,
createdAt: action.createdAt
}
];
case LOGOUT_SUCCESS:
return [];
default:
return state;
}
}
当我反复运行应用程序时,由于以下redux-logger错误,它大约失败了3次:
action ADD_ASSIGNMENT @ 19:02:09.732
prev state Object {router: Object, auth: Object, device: Object, global: Object, profile: Record…}
action Object {type: "ADD_ASSIGNMENT", assignmentId: "oNncJdVT16", shopperId: "aoieAPH6ll", itemId: "gza3038iPN", createdAt: Tue Feb 09 2016 11:43:48 GMT-0500 (EST)}
error TypeError: Cannot read property 'itemId' of null(…)
next state Object {router: Object, auth: Object, device: Object, global: Object, profile: Record…}
assignments: Array[1]
0: Object
assignmentId: "oNncJdVT16"
createdAt: Tue Feb 09 2016 11:43:48 GMT-0500 (EST)
itemId: "gza3038iPN"
shopperId: "aoieAPH6ll"
注意错误说明itemId为null,但该操作实际上包含非null itemId。下一个状态似乎是正确的,但应用程序在遇到此错误后无法恢复(不幸的是,它不会抛出带有其他信息的红屏)。
我还捕获了相同的redux-logger事件,显示了相同的操作,没有任何错误:
action ADD_ASSIGNMENT @ 19:01:50.601
prev state Object {router: Object, auth: Object, device: Object, global: Object, profile: Record…}
action Object {type: "ADD_ASSIGNMENT", assignmentId: "oNncJdVT16", shopperId: "aoieAPH6ll", itemId: "gza3038iPN", createdAt: Tue Feb 09 2016 11:43:48 GMT-0500 (EST)}
next state Object {router: Object, auth: Object, device: Object, global: Object, profile: Record…}
assignments: Array[1]
0: Object
assignmentId: "oNncJdVT16"
createdAt: Tue Feb 09 2016 11:43:48 GMT-0500 (EST)
itemId: "gza3038iPN"
shopperId: "aoieAPH6ll"
有关如何获得的任何建议?
答案 0 :(得分:0)
好的,想通了。我错误地称之为Redux"任务"减速机功能为"项目" (如我之前的代码片段所示)。
它表现出间歇性行为的原因是因为有两种可能的代码执行路径:
1)如果代码执行处理" ADD_ITEM"行动,然后"项目" reducer正确初始化"项目" redux数据存储区中的数据结构。
2)然而,如果" ADD_PROPOSAL"首先执行,然后执行"项目"数据结构未正确初始化,从而导致错误。