Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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 with trycatch

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

axios 200 goes into then and catch

this issue happen when your response have an error , for example when the response is correct but in the then response you do something that throw an error , axios then catch that error , even if the response from the server was success
Comment

PREVIOUS NEXT
Code Example
Javascript :: js concat variable and string 
Javascript :: quine 
Javascript :: change theme in react-toastify 
Javascript :: regexp object js 
Javascript :: jquery match height,jquery matchheight 
Javascript :: laravel return validation errors ajax 
Javascript :: jquery boilerplate 
Javascript :: trim string after - in jquery 
Javascript :: how to remove comma from array in javascript 
Javascript :: javascript replace dash with space 
Javascript :: jquery scroll to element id 
Javascript :: array to string javascript 
Javascript :: typescript read json file 
Javascript :: how to list node process 
Javascript :: open xcode shorthand react native 
Javascript :: exclude file types from formater vscode 
Javascript :: javascript url check 
Javascript :: how to eliminate decimals in js 
Javascript :: how to check if a javascript object is empty 
Javascript :: javascript class extends 
Javascript :: mongoose schema cast decimals 
Javascript :: capitalize first letter of string javascript 
Javascript :: jspdf save in server 
Javascript :: reset function javascript 
Javascript :: correct json type 
Javascript :: get value of radio button javascript 
Javascript :: short if 
Javascript :: show tooltip automatically 
Javascript :: javascript get current day of month 
Javascript :: how to make link in discord.js 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =