Vue.js - How to determine if an axios request has no response data

Viewed 1503

I am trying to test if the IP address provided by a user is the expected IP, so i send a request to that IP address which is expected to be running the API service, if the IP is correct it return success but if the IP is wrong it returns no response data, in the front-end of the application which is Vue.js i want to be able to know what happened and fire an alert to the user, if its successful i'm able to do that but if its not nothing happens, how do i get that error message?

this is my code

PublisherUrl().get('/api/publish-lv/').then(()=>{
           
            Toast.fire({
                icon: 'success',
                title: 'Publisher Connected Successfully'
            })
        }).catch((error)=>{
            console.log(error)
          
            Toast.fire({
                icon: 'error',
                title: 'Publisher Device Unreachable'
            })
           
        })
    }
1 Answers

the callback function inside then has a parameter response that returns the data, headers and status ...

PublisherUrl().get('/api/publish-lv/').then((response)=>{
  if(response.data.length===0){
     Toast.fire({
                icon: 'info',
                title: 'No data'
            })
  }else{
     Toast.fire({
                icon: 'success',
                title: 'Publisher Connected Successfully'
            })
  }

   }).catch((error)=>{
            console.log(error)
          
            Toast.fire({
                icon: 'error',
                title: 'Publisher Device Unreachable'
            })
           
        })
    }
Related