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

132
Views
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 answers
Answer question

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 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!