Javascript TypeError:无法读取属性';然后';关于未定义---我如何解决这个问题?
我不知道为什么我的函数会给我这个错误。在返回值的函数中,我确保在所有级别上都有一个return语句。关于为什么会发生这种错误,我能有第二种意见吗?服务完美地返回了数据,但是前端没有看到我的操作返回的数据 我尝试了其他同名的解决方案,所以希望这不是重复的解决方案 前端:Javascript TypeError:无法读取属性';然后';关于未定义---我如何解决这个问题?,javascript,reactjs,redux,es6-promise,Javascript,Reactjs,Redux,Es6 Promise,我不知道为什么我的函数会给我这个错误。在返回值的函数中,我确保在所有级别上都有一个return语句。关于为什么会发生这种错误,我能有第二种意见吗?服务完美地返回了数据,但是前端没有看到我的操作返回的数据 我尝试了其他同名的解决方案,所以希望这不是重复的解决方案 前端: componentDidMount() { const { item } = this.props; this.props.getDetails(Number) .then((res)
componentDidMount() {
const { item } = this.props;
this.props.getDetails(Number)
.then((res) => {
this.setState({
data: res.response.response.Results
})
});
}
映射:
function mapDispatchToProps(dispatch) {
return {
getDetails(Number) {
dispatch(getActions.getDetails(Number));
}
};
}
函数检索:
function getDetails(Number) {
return (dispatch) => {
dispatch({ type: policyConstants.ITEM_GETDETAIL_REQUEST });
return Service.getDetails(Number)
.then(
response => {
dispatch({ type: Constants.ITEM_GETDETAIL_SUCCESS });
var pDetails = response.response.Results;
return { pDetails };
},
error => {
dispatch({ type: policyConstants.POLICY_GETDETAIL_FAILURE, error});
}
);
}
}
您的组件中的this.props.getDetails(Number)
被计算为未定义的,因为mapDispatchToProps中的getDetails
不返回任何内容
像这样改变:
function mapDispatchToProps(dispatch) {
return {
getDetails(Number) {
return dispatch(getActions.getDetails(Number));
}
};
}
哪一行代码引发了该错误?@MattU。然后,在ComponentDidMount中的那一行,在映射函数中,您将getActions函数错误地称为“detActions.getDetails”。由mapDispatchToProps()返回的对象的getDetails
方法不返回任何内容。上一个代码段中的getDetails
函数确实返回了一个箭头函数,服务.getDetails
从未被调用。