我是使用 redux-pack 的新手,但喜欢 promise 方法真正清理代码并使每一步都非常清晰的方式。但是,我遇到了一个问题,承诺似乎消失并且错误被抛出。我的代码如下所示:
动作.js
import * as types from './action_types';
import * as services from '../services';
export const login = (email, password) ({type: types.LOGIN, promise: services.login(email, password)});
服务.js
const checkResponse = (response) => {
if (response.ok) {
return response;
} else {
let error = new Error(response.statusText);
error.response = response;
throw error;
}
};
export const login = (email, passowrd) => {
let request = new Request('/login', {method: 'POST', body: {email: email, password: password}});
return fetch('/login', request)
.then(checkResponse)
.then(response => response.json())
.catch(error => error);
};
减速器.js
import handle from 'redux-pack';
import * as actions from '../actions';
const INITIAL_STATE = {
loggedin: false,
isworking: false,
err: null,
};
export default function reducer(state = INITIAL_STATE, action) {
const { type, payload } = action;
switch (type) {
case actions.LOGIN:
return handle(state, action, {
start: prevState) => ({...prevState, isworking: true, err: null}),
finish: prevState => ({ ...prevState, isworking: false }),
failure: prevState => ({ ...prevState, err: payload }),
success: prevState => ({ ...prevState, loggedin: payload.ok })
});
default:
return state;
}
}
但是,当我运行此代码时,触发操作时出现以下错误:
TypeError: Object is not a function (near '...(0, _reduxPack2.default)...')
我已经使用控制台输出对代码进行了检测,并且可以看到以下步骤直接发生在错误之前:
[Log] Login login, called +0ms (client.js, line 7245)
[Log] Login props = {"match":{"path":"/login","url":"/login","isExact":true,"params":{}},"location":{"pathname":"/login","state":{"from":"/authRoute"},"search":"","hash":"","key":"6gnu0d"},"history":{"length":72,"action":"REPLACE","location":{"pathname":"/login","state":{"from":"/authRoute"},"search":"","hash":"","key":"6gnu0d"}},"isworking":false,"loggedin":false,"err":null} +1ms (client.js, line 7245)
[Log] Login mapDispatchToProps, login dispatched. +0ms (client.js, line 7245)
[Log] actions login, called +0ms (client.js, line 7245)
[Log] services login, called. +0ms (client.js, line 7245)
[Log] actions promise = [object Promise] +2ms (client.js, line 7245)
[Log] actions action = {"type":"LOGIN","promise":{}} +0ms (client.js, line 7245)
[Log] reducer reducer called +4s (client.js, line 7245)
[Log] reducer state is: {"loggedin":false,"isworking":false,"err":null} +0ms (client.js, line 7245)
[Log] reducer action type is: "LOGIN" +0ms (client.js, line 7245)
[Log] reducer action promise is: undefined +0ms (client.js, line 7245)
[Log] reducer action payload is: undefined +0ms (client.js, line 7245)
我在减速器的启动函数中也有一些预期的输出,并且从未被调用过。在上面的“登录”是 React 组件,我可以看到它发出调用,道具具有预期的状态,并且登录调用被调度。然后我看到操作调用服务,获取生成的承诺并构建完整的操作。调用reducer 并且状态看起来如预期,动作类型如预期但动作的承诺部分未定义,我怀疑这可能是导致对react-pack处理程序的调用引发错误的原因。欢迎任何关于尝试什么或在哪里寻找的建议!