Javascript 我怎样才能得到调度的答复?
我有一个组件,它有一个表单,此时在提交按钮上执行clic,我调用一个函数handleSubmit它在我的组件上,这个函数调用一个动作,通过分派和这个动作,我调用一个服务HTTP请求 手推 actions.addDevice 服务添加设备Javascript 我怎样才能得到调度的答复?,javascript,reactjs,react-redux,Javascript,Reactjs,React Redux,我有一个组件,它有一个表单,此时在提交按钮上执行clic,我调用一个函数handleSubmit它在我的组件上,这个函数调用一个动作,通过分派和这个动作,我调用一个服务HTTP请求 手推 actions.addDevice 服务添加设备 我希望组件中的响应能够进行验证,但由于请求是异步的,我永远无法获得响应,只能打印未定义的变量。如何获得同步响应?或者我需要做什么才能进行验证?您没有返回promise service.addDevice 所以你可以做退货服务。添加设备。。。在handleSubm
我希望组件中的响应能够进行验证,但由于请求是异步的,我永远无法获得响应,只能打印未定义的变量。如何获得同步响应?或者我需要做什么才能进行验证?您没有返回promise service.addDevice 所以你可以做退货服务。添加设备。。。在handleSubmit中,您执行分派…然后data=>…对数据执行某些操作 这是异步的。因此,从console.log返回undefined并不奇怪。即使在分派过程完成之前也要执行console.log。使用承诺或异步等待语法。我建议使用异步等待语法
handleSubmit = (e) => {
e.preventDefault()
const { validateFields } = this.props.form;
validateFields(async (err, params) => {
if (!err) {
const { user, dispatch } = this.props;
let response =await dispatch(actions.addDevice(params))
console.log(response); //Response is undefined
}
});
}
请用此代码替换您的代码 手推 actions.addDevice
要在组件中使用响应,您还需要做3件事:根据ADD_DEVICE_请求生成一个reducer以更新redux状态,生成一个选择器函数以获取在该状态中更改的变量,然后向组件添加一个prop,该prop等于使用mapstatetops函数生成的选择器的结果
function addDevice(params){
return (dispatch, getState) => {
let { authentication } = getState();
dispatch(request({}));
service.addDevice(params, authentication.user.access_token)
.then(
response => {
if(response.status === 201) {
dispatch(success(response.data));
}
return response;
},
error => {
dispatch(failure(error.toString()));
dispatch(alertActions.error(error.toString()));
}
)
}
function request(response) { return { type: constants.ADD_DEVICE_REQUEST, response } }
function success(response) { return { type: constants.ADD_DEVICE_SUCCESS, response } }
function failure(error) { return { type: constants.ADD_DEVICE_FAILURE, error } }
}
function addDevice(params, token){
return axios({
url: 'http://localhost:5000/user/add-device',
method: 'POST',
headers: { 'Authorization': 'Bearer ' + token},
data: {
data1: params.data1,
data2: params.data2,
data3: params.data3
}
})
.then(function(response) {
return response;
})
.catch(function(error) {
return error.response;
});
}
let response = dispatch(actions.addDevice(params))
handleSubmit = (e) => {
e.preventDefault()
const { validateFields } = this.props.form;
validateFields(async (err, params) => {
if (!err) {
const { user, dispatch } = this.props;
let response =await dispatch(actions.addDevice(params))
console.log(response); //Response is undefined
}
});
}
handleSubmit = (e) => {
e.preventDefault()
const { validateFields } = this.props.form;
validateFields((err, params) => {
if (!err) {
const { user, dispatch } = this.props;
dispatch(actions.addDevice(params)).then((response)=>{
console.log(response);
})
}
});
}
function addDevice(params){
return (dispatch, getState) => {
let { authentication } = getState();
dispatch(request({}));
return service.addDevice(params, authentication.user.access_token)
.then(
response => {
if(response.status === 201) {
dispatch(success(response.data));
}
return response;
},
error => {
dispatch(failure(error.toString()));
dispatch(alertActions.error(error.toString()));
}
)
}
function request(response) { return { type: constants.ADD_DEVICE_REQUEST, response } }
function success(response) { return { type: constants.ADD_DEVICE_SUCCESS, response } }
function failure(error) { return { type: constants.ADD_DEVICE_FAILURE, error } }
}