Redux reducer 不显示我的控制台日志并返回未定义
Redux reducer not showing my console log and returning undefined
嗨,自从我将 JWT 添加到我的巢 API 后,我的 Redux 应用程序出现了问题,
这是我的减速器的代码:
export default function reducer(state = {}, action) {
switch (action.type) {
case 'USER_LOGIN_SUCCESS':
console.log('USER_LOGIN_SUCCESS')
return action.payload.user;
case 'USER_REGISTER_SUCCESS':
// user exist
if(action.payload.status === 409){
console.log(action.payload)
return state;
}
console.log(action.payload)
return state
case 'USER_REGISTER_FAILURE':
case 'USER_LOGIN_FAILURE':
default:
console.log('lol');
return state
}
}
当我使用 redux-api-middleware 登录到我的 API 时,这就是我在控制台中获得的内容:
redux.js:463 Uncaught (in promise) Error: Given action "USER_LOGIN_SUCCESS", reducer "user" returned undefined. To ignore an action, you must explicitly return the previous state. If you want this reducer to hold no value, you can return null instead of undefined.
at combination (redux.js:463)
at dispatch (redux.js:213)
at _callee$ (index.umd.js:2874)
at s (index.js:1)
at Generator._invoke (index.js:1)
at Generator.e.<computed> [as next] (index.js:1)
at asyncGeneratorStep (index.umd.js:33)
at _next (index.umd.js:55)
当我用 POSTMAN 尝试我的请求时,它完美地工作了:/
问题是它甚至没有在我的减速机案例中显示 console.log :'USER_LOGIN_SUCCESS'
这是请求对 POSTMAN 的响应:
POSTMAN RESPONSE
在您的 USER_LOGIN_SUCCESS
案例中,您正在 return 操作负载值。
使用 reducer,你必须始终 return 状态对象或状态对象的不可变副本。
不清楚你的 reducer 状态对象是什么样的,但我推测它包含一个用户 属性
return {
...state,
user: action.payload.user
}
嗨,自从我将 JWT 添加到我的巢 API 后,我的 Redux 应用程序出现了问题, 这是我的减速器的代码:
export default function reducer(state = {}, action) {
switch (action.type) {
case 'USER_LOGIN_SUCCESS':
console.log('USER_LOGIN_SUCCESS')
return action.payload.user;
case 'USER_REGISTER_SUCCESS':
// user exist
if(action.payload.status === 409){
console.log(action.payload)
return state;
}
console.log(action.payload)
return state
case 'USER_REGISTER_FAILURE':
case 'USER_LOGIN_FAILURE':
default:
console.log('lol');
return state
}
}
当我使用 redux-api-middleware 登录到我的 API 时,这就是我在控制台中获得的内容:
redux.js:463 Uncaught (in promise) Error: Given action "USER_LOGIN_SUCCESS", reducer "user" returned undefined. To ignore an action, you must explicitly return the previous state. If you want this reducer to hold no value, you can return null instead of undefined.
at combination (redux.js:463)
at dispatch (redux.js:213)
at _callee$ (index.umd.js:2874)
at s (index.js:1)
at Generator._invoke (index.js:1)
at Generator.e.<computed> [as next] (index.js:1)
at asyncGeneratorStep (index.umd.js:33)
at _next (index.umd.js:55)
当我用 POSTMAN 尝试我的请求时,它完美地工作了:/ 问题是它甚至没有在我的减速机案例中显示 console.log :'USER_LOGIN_SUCCESS'
这是请求对 POSTMAN 的响应: POSTMAN RESPONSE
在您的 USER_LOGIN_SUCCESS
案例中,您正在 return 操作负载值。
使用 reducer,你必须始终 return 状态对象或状态对象的不可变副本。
不清楚你的 reducer 状态对象是什么样的,但我推测它包含一个用户 属性
return {
...state,
user: action.payload.user
}