In the below code, a 404 status is sent up to the front end causing the res.ok
property to be false. Is there a way to have access to this message from the backend in the front end if statement where the res.ok
value is checked?
app.post('/recover_password', (req, res) => {
const customer_has_account = true
if (!customer_has_account) {
return res.status(404).json('no account found with that email')
}
})
fetch('/recover_password', {
method: 'POST',
body: /* ... */
})
.then(res => {
if (!res.ok) {
throw new Error(/* can error be passed in here somehow? */)
}
return res.json()
})
.then(data => console.log(data))
.catch(err => {
console.log(err)
})
I suppose this would also work but I am wondering if there is a specific way to do this as shown above
app.post('/recover_password', (req, res) => {
const customer_has_account = true
if (!customer_has_account) {
return res.status(404).json({ error: 'no account with that email found '})
}
})
fetch('/recover_password', {
method: 'POST',
body: /* ... */
})
.then(res => res.json())
.then(data => {
if (data.error) throw new Error(data.error)
})
.catch(err => console.log(err))