我想调用我的rest api。但我收到了这个错误:
restApi.js:
import {customerbyidGetAction} from '../../redux/actions/restActions'
export default function customerbyidGet(dispatch) {
return () => {
dispatch(customerbyidGetAction());
fetch('https://***.com/api/v1/home',{
method:'GET',
headers:{
'Accept': 'application/json',
'Content-Type': 'application/json',
'token':1234
}//,
// body: JSON.stringify({'customerId': 1})
})
.then(res => {
const r = res.json();
return r;
})
.then(res => {
if(res.error) {
throw(res.error);
}
console.log('then rest')
dispatch(customerbyidGetAction());
return res;
})
.catch(error => {
console.log('r456r'+error);
dispatch(customerbyidGetAction());
})
}
}
面板.js
import customerbyidGet from '../../../api/rest/restApi';
class Panelclass extends React.Component {
componentDidMount(){
const {customerbyidGet} = this.props;
customerbyidGet()
}
render() {
return (
<div>
hi
</div>
);
}
}
Panelclass.propTypes = {
classes: PropTypes.object.isRequired,
customerbyidGet: PropTypes.func.isRequired,
};
const mapStateToProps = (state) => ({ data: {} });
const mapDispatchToProps = dispatch => bindActionCreators({
customerbyidGet: customerbyidGet(dispatch)
}, dispatch)
const Panel = connect(
mapStateToProps,
mapDispatchToProps
)(Panelclass);
export default withStyles(styles)(Panel);
restActions.js
import * as types from '../constants/restConstants';
export const customerbyidGetAction = (data = { "default value": "some value" }) => ({ type: types.customerbyidGetAction, payload:data });
restReducer.js
import * as types from '../constants/restConstants';
const initialState = {};
const initialImmutableState = fromJS(initialState);
export default function restReducer(state = initialImmutableState, action) {
switch(action.type) {
case types.customerbyidGetAction:
return {
...state,
data: action.payload
}
default:
// the dispatched action is not in this reducer, return the state unchanged
return state;
}
}