Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

221
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda