简体   繁体   English

未处理的拒绝(TypeError):分派不是在动作创建者中获取此错误的函数

[英]Unhandled Rejection (TypeError): dispatch is not a function getting this error in action creator

I am getting this error while dispatch is called. 在调用dispatch时出现此错误。

Unhandled Rejection (TypeError): dispatch is not a function

here is my action creator code where I am getting above error. 这是我的动作创建者代码,上面出现错误。

export function getStatus(requestId) {
  return async dispatch => {
    let url = GET_UPLOADED_STATUS_URL + requestId;
    let response = await get(url);
    const timeout = setTimeout(getStatus(requestId), 2000);
    if (response.status === 200) {
      let total = response.payload.count;
      let totalCompleted = response.payload.uploadCompleted
      dispatch({
        type: UPDATE_PROGRESS_BAR_STATUS,
        total: total,
        completed: totalCompleted
      })
      if (!response.payload == "") {
        toastr.info(`Total processed ${totalCompleted}`);
      }
      if (response.payload === "") {
        dispatch({
          type: SHOW_PROGRESS_BAR,
          data: false
        })
        clearTimeout(timeout);
      }
    } else {
      clearTimeout(timeout);
      dispatch({
        type: SHOW_PROGRESS_BAR,
        data: false
      });
    }
  }
}

As you can see I am calling this action in every two second.Its work fine for first time execution but while executing second time its through above error while dispatching UPDATE_PROGRESS_BAR_STATUS. 如您所见,我每两秒钟调用一次此动作,它可以在第一次执行时正常工作,但是在第二次通过上述错误执行时却调度了UPDATE_PROGRESS_BAR_STATUS。 and in console getting this error. 并在控制台中收到此错误。

Uncaught (in promise) TypeError: dispatch is not a function
    at _callee3$ (index.js:100)

can someone please guide me what I am doing wrong here or why I am getting this error.any help will be appreciated thanks in advance. 有人可以指导我在这里做错什么或为什么我收到此错误吗?任何帮助将不胜感激,谢谢。

here is my updated code where how I am calling getStatus() action. 这是我更新的代码,在这里我如何调用getStatus()操作。

export function uploadFileFolder(arrayofFile, jdId) {
  return async dispatch => {
    dispatch({
      type: REQUEST_INITIATED
    });
    let url = UPLOAD_FILE_FOLDER_URL + jdId;
    let response = await post(url, arrayofFile);
    if (response.status === 200) {
      dispatch({
        type: REQUEST_SUCCESSED
      });
      history.push({
        pathname: "/file-list/" + jdId
      });
      toastr.success(response.payload.message);
      dispatch({
        type: SHOW_PROGRESS_BAR,
        data: true
      });
      let requestId = response.payload.requestId;
      dispatch(getStatus(requestId));
    } else {
      dispatch({
        type: REQUEST_SUCCESSED
      });
    }
  }
}

and uploadFileFolder() action I am calling this by click on button in my component. 和uploadFileFolder()操作我通过在组件中单击按钮来调用它。

You getting this error because when you calling getStatus() action after every 2 seconds is not getting dispatch. 之所以收到此错误,是因为每2秒调用一次getStatus()操作不会得到调度。 As you said its working fine for first-time execution but while calling the 2nd time it's through an error because for the second time it's not getting dispatch. 正如您所说的,它在第一次执行时可以正常工作,但是在第二次调用时却遇到了错误,因为第二次没有得到调度。 So you need to update your code like given below. 因此,您需要像下面给出的那样更新代码。

const timeout = setTimeout(dispatch(getStatus(requestId), 2000));

I Think there is something wrong with the connect code. 我认为连接代码有问题。 I am not sure How did you connect it. 我不确定您如何连接它。 Please follow this. 请遵循此。 Hope it will work. 希望它能工作。

    //YOUR COMPONENT FILE
    import {uploadFileFolder} from 'path/to/your/uploadFileFolderFile' 

    export class YourComponent .... {
      //Your Component Code

      onButtonClick() {
        //OtherCode
        this.props.uploadFileFolder(arrayofFile, jdId)
      }

    }
    const mapStateToProps = (state) => {
      return {
      };
    };

    export default connect(mapStateToProps, { uploadFileFolder })(YourComponent);


    //Action File
    export const uploadFileFolder => (arrayofFile, jdId) => {
      return async dispatch => {
        //...Rest of the code
      };
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试调度一个操作,但收到此错误:“未处理的拒绝(TypeError):无法读取未定义的属性'类型'” - I am trying to dispatch a action but getting this error: “ Unhandled Rejection (TypeError): Cannot read property 'type' of undefined” 未处理的拒绝(TypeError):调度不是 function 反应 js - Unhandled Rejection (TypeError): dispatch is not a function react js Redux 动作创建者收到“TypeError: dispatch(...) is not a function”(附上代码)? - Redux action creator getting "TypeError: dispatch(...) is not a function" (code attached)? 在 ReactJS 中获取错误为“未处理的拒绝(TypeError):that.setState 不是函数” - Getting error as “Unhandled Rejection (TypeError): that.setState is not a function” in ReactJS 出现错误:“可能未处理的 Promise 拒绝(id:2):类型错误:未定义不是 function(靠近 '...myList.map...')” - Getting error : “Possible Unhandled Promise Rejection (id: 2): TypeError: undefined is not a function (near '…myList.map…')” 未处理的拒绝(TypeError):setToken 不是函数 - Unhandled Rejection (TypeError): setToken is not a function 未处理的拒绝 (TypeError):setDailyData 不是函数 - Unhandled Rejection (TypeError): setDailyData is not a function 未处理的拒绝(TypeError):Object(...) 不是函数 - Unhandled Rejection (TypeError): Object(...) is not a function 未处理的拒绝(TypeError):setX 不是 function - Unhandled Rejection (TypeError): setX is not a function 反应错误:未处理的拒绝(类型错误):this.state.categories.map 不是 function - React error : Unhandled Rejection (TypeError): this.state.categories.map is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM