I am fetching data from a api end point. It takes username,email,password,firstname,lastname and register it to database. Now if some error occurs like email already used then it gives a 400 status code and sends this
{
"err": "A user with the given email is already registered"
}
so I want to use this api endpoint. In react js I am doing this.
const printError=(err)=>{
if((typeof(err)=='string'))
return err
else if(err.err!=undefined)
return err.err
else if(err.message!=undefined)
return err.message
else
return 'Some error occured.Try Again later.'
}
fetch(BaseUrl+'users/signup',{
method:'post',
credentials:'include',
body:JSON.stringify(user),
headers:{
'content-type':'application/json'
}
})
.then(response=>{
console.log(response)
if(response.ok)
return response.json()
else{
throw new Error(response)
}
})
.then(response=>{
alert(response.status)
})
.catch(error => { console.log('Registration error');console.log(error); alert('Registration Failed\nError: '+printError(error)); });
Please tell me how to get this err object when email is already taken.