微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法使用 redux

如何解决无法使用 redux

我使用 mern 堆栈设计了一个 CRM 工具,当我尝试从 mongoDB 检索与项目阶段相关的特定信息时,我看到了以下错误

getStages api error: 
TypeError: action.payload is not iterable
    at stageReducer (stageReducers.js:11)
    at combination (redux.js:528)
    at computeNextEntry (<anonymous>:3395:21)
    at recomputeStates (<anonymous>:3429:17)
    at <anonymous>:3809:22
    at Object.dispatch (redux.js:288)
    at dispatch (<anonymous>:3856:17)
    at index.js:11
    at dispatch (redux.js:659)
    at stageAction.js:7

另外,我在这个实现中使用了 redux。我实现了这个功能的后端并用邮递员测试它并且它完美地工作 但前端没有按预期检索信息。 stageReducer、stageAction 和 index.js 文件如下:

stageReducer:

import { GET_STAGES } from "../constants/stageConstants";

const INITIAL_STATE = {
    stages: []
};

const stageReducer = (state=INITIAL_STATE,action) => {
    switch(action.type){
        case GET_STAGES:
            return{
                stages: [...action.payload]
            };
        default:
            return state;
    }
};

export default stageReducer;

舞台动作:

import { GET_STAGES } from "../constants/stageConstants";
import axios from "axios";

export const getStages = () => async dispatch =>{
    try{
        const response = await axios.get('/stages');
        dispatch({
            type: GET_STAGES,payload: response.data.stages
        });
        console.log(response);
    }catch(err){
        console.log('getStages api error:',err);
    }
}

index.js

import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import App from "./components/App";
import reportWebVitals from "./reportWebVitals";
import { Provider } from 'react-redux';
import store from './redux/store';

ReactDOM.render(
  <React.StrictMode>
    <Provider store ={store}>
      <App/>
    </Provider>
    </React.StrictMode>,document.getElementById("root")
);

reportWebVitals();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。