[Vuejs]-Error handling with axios method in vuejs

0👍

First of all, you should either wrap your axios request with try/catch if using async/await, or simply use then..catch methods.

Here’s a simple example

axios.get('http://api.com')
.then((response) => {
 if (response.data.length === 0) { // Lets check if response contains any items
   // Do Your 'no items found' logic here
   console.log('No items found')
   return
 } else { // We have items, handle them here!
   // We have some items, lets log them
   console.log(response.data)
 }
})
.catch((error) => {
  // Catch and handle any errors here
  console.log(error)
})

Leave a comment