当尝试在 getInitialProps 中调用 API 端点(调度 redux 操作)时,我正在使用 next js。我收到 404 错误,我的 /api/ 在 nginx 服务器中被代理,所有其他路由都工作得很好,只有这个路由导致了问题。
我尝试将 api fetch 函数调用更改为异步,但仍然出现相同的错误。
static async getInitialProps({ store, query: { id } }) {
await store.dispatch(fetchP(id));
console.log('GetIntial');
return { id };
}
Run Code Online (Sandbox Code Playgroud)
我的动作创造者
export const fetchp = id => async dispatch => {
dispatch({ type: FETCH_P_DETAILS_STARTED });
await API.get(`ps/${id}`)
.then(res => {
console.log(res);
dispatch({
type: FETCH_P_DETAILS_SUCCESS,
payload: res.data.data,
});
})
.catch(err => {
console.log(err);
if (!err.response) {
// network error
err.message = 'Error: Network Error'; …Run Code Online (Sandbox Code Playgroud)