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

134
Visualizações
show only the message of error object in fetch api javascript

I want to log the error message in the console. Everything is working fine except that I could not log only the message for eg if the email already exists it should log "email exists"

const submitHandler = async(event) => {
    event.preventDefault();
    const enteredEmail = emailInputRef.current.value;
    const enteredPassword = passwordInputRef.current.value;
    try {
      if (isLogin) {
           //do something
      } 
      
      else {
        setstate("sending request")
        const response = await fetch(
          "https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=xxx",
          {
            method: "POST",
            body: JSON.stringify({
              email: enteredEmail,
              password: enteredPassword,
              returnSecureToken: true,
            }),
            headers: {
              "Content-Type": "application/json",
            },
          }
          )
        if(response.ok){
          alert("User added successfully")  
        }
        else{
          const data = await response.json()
          console.log(data);
          throw new Error(data);
        }
        setstate("Create new account")
      }
      
    } catch (error) {
      console.log(error.message);
    }
  };
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Instead of throwing an error with data, just throw an error with string message. Ref

throw new Error(data.message); // if data look like { message: 'email exists' }
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