Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

225
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda