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 how to get error response

axios.post('/formulas/create', {
	name: "",
	parts: ""
})
.then(response => { 
	console.log(response)
})
.catch(error => {
    console.log(error.response)
});
Comment

axios error message

        .catch((error) => {
		  const errorMessage = error?.response?.data?.message;
          const message = errorMessage || "your custome message";
          this.$toast.show(message, {
            icon: "info",
            className: "toast-danger",
          });
		})
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 add items to array 
Javascript :: how to import in react js 
Javascript :: jQuery intellisence in VSCode 
Javascript :: UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON 
Javascript :: how to add objects in array 
Javascript :: javascript ajax get 
Javascript :: how to receive window.postmessage event in angular 9 
Javascript :: react vscode stop auto close tag 
Javascript :: findone and update mongoose 
Javascript :: reactjs change fill color .svg 
Javascript :: how to create a object in javascript 
Javascript :: apollo client nextjs 
Javascript :: pdf.js extract text 
Javascript :: how to do jest unit test in react 
Javascript :: 1 day ago javascript 
Javascript :: detect dark mode 
Javascript :: javascript hoisting 
Javascript :: map in react 
Javascript :: material ui react card 
Javascript :: arrow function 
Javascript :: my angular modal popup is not closing automatically 
Javascript :: react barcode scanner 
Javascript :: how to use data sets javascrip[t 
Javascript :: ajax laravel get values from form 
Javascript :: toggle 
Javascript :: $$ promise then 
Javascript :: get largest no in the array javascript 
Javascript :: javascript do while array 
Javascript :: threejs perspectivecamera 
Javascript :: react routes multiple compoenents 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =