Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
Cannot console log DRF error message in react

I'm struggling to figure out how to console log an error I am getting from my django-rest framework. This is what I currently have

const onSubmit = (data) => {
    const user = {
        username: data.username,
        email: data.email,
        password: data.password,
        profile: {
                    city: data.city,
                    country: data.country,
        }
    };
    fetch('http://127.0.0.1:8000/api/register', {
        method: 'POST',
        headers: {
            'Content-Type':'application/json'
        },
        body: JSON.stringify(user)
        })
        .then(res => res.json())
            .then((data) => {
            })
            .catch((resp)=>{console.log(resp)})
    };

This is an example of an error I am getting:

{"username":["A user with that username already exists."]}

This is a 400 Error

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

this is your console.log() ==> {"username":["A user with that username already exists."]} to catch your error message from your server. If your goal is to test your application. Don't use the same username or clear the user from the database first.

about 4 years ago · Juan Pablo Isaza Report

0

Figured out that the error message was in a promise so I had to resolve the promise to be able to console the error message:

const onSubmit = (data) => {
    const user = {
        username: data.username,
        email: data.email,
        password: data.password,
        profile: {
                    city: data.city,
                    country: data.country,
        }
    };
    fetch('http://127.0.0.1:8000/api/register', {
        method: 'POST',
        headers: {
            'Content-Type':'application/json'
        },
        body: JSON.stringify(user)
        })
        .then(res => {
            if(!res.ok){
               return res.text().then(text=> {console.log(text) })
            }
            else {
                return res.json().then(data=> {console.log(data) })
                }}) }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!