Reactjs 在react中调用api时,我无法读取未定义的属性'then'
我得到TypeError:在react.js和redux中调用api时无法读取未定义的属性'then' 我的组件安装功能是-Reactjs 在react中调用api时,我无法读取未定义的属性'then',reactjs,redux,Reactjs,Redux,我得到TypeError:在react.js和redux中调用api时无法读取未定义的属性'then' 我的组件安装功能是- componentDidMount() { window.scrollTo(0, 0); var requestedId = this.props.match.params.id; this.props.fetchCategoryJoblist(requestedId).then(() => { this.setState({ l
componentDidMount() {
window.scrollTo(0, 0);
var requestedId = this.props.match.params.id;
this.props.fetchCategoryJoblist(requestedId).then(() => {
this.setState({ loading: false });
});
}
我在此行this.props.fetchCategoryJoblistrequestedId获得无法读取的then属性
我的组件安装函数-
componentDidMount() {
window.scrollTo(0, 0);
var requestedId = this.props.match.params.id;
this.props.fetchCategoryJoblist(requestedId).then(() => {
this.setState({ loading: false });
});
}
My action.js文件-
// code to get job listing based on category
export function setCategoryJoblist(categoryjoblist) {
return {
type: SET_CATEGORY_JOBLIST,
categoryjoblist
};
}
export function fetchCategoryJoblist(requestedId) {
var apiUrl = `http://18.207.190.61:4000/getJobByCategory/${requestedId}`;
return dispatch => {
fetch(apiUrl)
.then(res => res.json())
.then(data => dispatch(setCategoryJoblist(data.Jobs)));
};
}
那么如何在componentDidMount方法中使用then方法呢?您的FetchCategory作业列表
export function fetchCategoryJoblist(requestedId) {
var apiUrl = `http://18.207.190.61:4000/getJobByCategory/${requestedId}`;
return dispatch => {
// see here
fetch(apiUrl)
.then(res => res.json())
.then(data => dispatch(setCategoryJoblist(data.Jobs)));
};
}
也许问题就在这里
export function fetchCategoryJoblist(requestedId) {
var apiUrl = `http://18.207.190.61:4000/getJobByCategory/${requestedId}`;
return dispatch => {
// you don't have return here, so your call to API is won't return a promise call CMIIW
return fetch(apiUrl)
.then(res => res.json())
.then(data => dispatch(setCategoryJoblist(data.Jobs)));
};
}
那么,我应该在这里做什么呢?尝试添加一个回访,看看我的猜测是否正确,或者在你的应用程序上是否可以调用。然后立即在componentDidMount上运行?你可以记录这一行吗?console.logthis.props.fetchCategoryJobListRequesteId