[英]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.