1

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.

2 Answers 2

3

Sorry for asking and telling the answer myself. The answer to this is throw response.json() if response.ok is false.

Sign up to request clarification or add additional context in comments.

Comments

0

fetch provides a simple ok flag that indicates whether an HTTP response’s status code is in the successful range or not. Please refer below link for more details:

https://www.tjvantoll.com/2015/09/13/fetch-and-errors/

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful

.then(response=>{
        console.log(response)// this is response from server, including all ok and error responses 
        if(response.ok)
            return response.json()
        else{
            console.log(response) ///error message for server should be in this response object only
        }
    })

4 Comments

through this ok flag I came to know that my response is not success.but the reason for that is given in the response body so how do i access it.In both the link that you have provided none contain the solution to that
The error from server should be in response only (throw new Error(response)), try something like.. response.statustext .... console response if it is not ok, error is there in the response object...
In response.statustext it will contain bad request .But I want to access{err:"email already taken...."}.If this is not possible with fetch tell me another way.
Check if server is sending error message, the response object should have message sent by server, console and try to see response and it's nested objects, like resopnse.startusText, there should be more info in response only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.