I am trying to use Django-rest-auth, I am getting it to work but often I get Bad request HTTP 400. In Django-rest-auth generated view I can see the details of the error (i.e username contains unaccepted chars or pwd do not match).
How can I get this information on frontend js side ? For now I was trying to just console the whole response but cannot find it in there
const registerUser = async (user ) => {
console.log( JSON.stringify(user))
const response = await fetch("/api/dj-rest-auth/registration/", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(user)
});
if (response.status === 201) {
console.log('did it ! !!! !! !!! ! !! ')
let data = response.json()
localStorage.clear();
localStorage.setItem('authToken', data.key);
} else {
console.log(response);
}
};
CodePudding user response:
Use try...catch
as stated in comments by Konrad Linkowski.
const registerUser = async (user ) => {
console.log( JSON.stringify(user))
try {
const response = await fetch("/api/dj-rest-auth/registration/", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(user)
});
if (response.status === 201) {
console.log('did it ! !!! !! !!! ! !! ')
let data = response.json()
localStorage.clear();
localStorage.setItem('authToken', data.key);
} else {
console.log(response);
}
}catch (e){
console.log(e) // log the error , its an object so you can get desired error message
}
};