Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to handle error axios js

axios.post("/api/end", {data : "xx"})
  .then(({ data }) => {
  // doing something with success
  })
  .catch((err) => {
  let message = typeof err.response !== "undefined" ? err.response.data.message : err.message;
  console.warn("error", message);
});
Comment

axios call error handling

axios.get('/api/xyz/abcd')
  .catch(function (error) {
    if (error.response) {
      // Request made and server responded
      console.log(error.response.data);
      console.log(error.response.status);
      console.log(error.response.headers);
    } else if (error.request) {
      // The request was made but no response was received
      console.log(error.request);
    } else {
      // Something happened in setting up the request that triggered an Error
      console.log('Error', error.message);
    }

  });
Comment

axios try catch

try {
    const { data } = await axios({
        method: 'put',
        url: '/api/article/123',
        data: {
            title: 'Making PUT Requests with Axios',
            status: 'published'
        }
    });

    console.log(data);
} catch (err) {
    if (err.response.status === 404) {
        console.log('Resource could not be found!');
    } else {
        console.log(err.message);
    }
}
Comment

axios error

axios.get('/api/xyz/abcd')
  .catch(function (error) {
    if (error.response) {
      // Request made and server responded
      console.log(error.response.data);
      console.log(error.response.status);
      console.log(error.response.headers);
    } else if (error.request) {
      // The request was made but no response was received
      console.log(error.request);
    } else {
      // Something happened in setting up the request that triggered an Error
      console.log('Error', error.message);
    }

  });
Comment

axios error handling

const onSubmit = async (FormData) => {
  FormData.bFlats = meters;
  try {
    let res = await axios({
      method: 'post',
      url: 'http://localhost:5000/api/v1/buildings',
      data: FormData
    });

    console.log(res.data);
    if (res.data.status === 'success') {
      alert("Successfully Inserted");
      reset();
    }
  } catch (error) {
    console.log(error.response.data.message); // this is the main part. Use the response property from the error object
  }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript window size 
Javascript :: javascript string search second occurrence 
Javascript :: js id generator 
Javascript :: js clone element 
Javascript :: redux devtools 
Javascript :: react use same useState for multiple inputs 
Javascript :: Package path ./compat is not exported from 
Javascript :: creating a class in angular 
Javascript :: javascript array push middle 
Javascript :: loop through object in array javascript 
Javascript :: javascript disable button 
Javascript :: you should not use switch outside a router react 
Javascript :: contains substring javascript 
Javascript :: nodejs check if string matches regex 
Javascript :: js export multiple functions 
Javascript :: jquery get select name value 
Javascript :: How to Perform Date Comparison With the Date Object in JavaScript 
Javascript :: chrome add a javascript bookmark 
Javascript :: ajax form picture upload 
Javascript :: js capitalize 
Javascript :: find one with specofoc id mongoose 
Javascript :: tsconfig.json not generated 
Javascript :: latin science words 
Javascript :: react aws s3 npm 
Javascript :: How to get input file using js 
Javascript :: angular build aot vs jit 
Javascript :: how to validate file type in jquery 
Javascript :: check if the document is ready js 
Javascript :: javascript check if variable is object 
Javascript :: Removing Service Workers Programmatically 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =