I have a register page. I want the user to be able to agree to sign up to a newsletter by checking off a box. I have written this code to handle the submit:
const handleSubmit = async e => {
e.preventDefault()
const errMsg = valid(name, email, password, cf_password)
if(errMsg){
return dispatch({type: 'NOTIFY', payload: {error: errMsg}})
}
dispatch({type: 'NOTIFY', payload: {loading: true}})
if(checked === 'on'){
const res = await postData('subscribe', userData)
if(res.err){
return dispatch({type: 'NOTIFY', payload: {error: res.err}})
}
return dispatch({type: 'NOTIFY', payload: {success: res.msg}})
}
const res = await postData('auth/register', userData)
if(res.err){
return dispatch({type: 'NOTIFY', payload: {error: res.err}})
}
return dispatch({type: 'NOTIFY', payload: {success: res.msg}})
}
When the user submits now he is only subscribing to the newsletter and not also registering, because a response has already been sent. Is there a way to send two responses to different api endpoints in the same submit function?
Solution: remove the returns in the if checked statement.
const handleSubmit = async e => {
e.preventDefault()
const errMsg = valid(name, email, password, cf_password)
if(errMsg){
return dispatch({type: 'NOTIFY', payload: {error: errMsg}})
}
dispatch({type: 'NOTIFY', payload: {loading: true}})
if(checked === 'on'){
const res = await postData('subscribe', userData)
if(res.err){
dispatch({type: 'NOTIFY', payload: {error: res.err}}) // removed return here
}
dispatch({type: 'NOTIFY', payload: {success: res.msg}}) // and here
}
const res = await postData('auth/register', userData)
if(res.err){
return dispatch({type: 'NOTIFY', payload: {error: res.err}})
}
return dispatch({type: 'NOTIFY', payload: {success: res.msg}})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.