[Vuejs]-Return an array from axios?

0👍

computed property can not have asynchronous tasks like making a backend call, you should use methods instead of computed property to call this method: getFilteredTrips.

You can have a data: filteredTrips, which you can update in the getFilteredTrips function. You code should look like something:

data:{
  return {
    filteredTrips: []
  }
},
computed: {
    filteredTripsComputed: function () {
        var filtered = this.trips;
        if(this.filters_by.indexOf('monitor')>=0) filtered = this.filter(filtered, this.BusMonitorID,'BusMonitorID');
        if(this.filters_by.indexOf('route')>=0) filtered =  this.filter(filtered,  this.TourID,'TourID');
        return filtered;
    }
},
methods: {
    filteredTrips:function(){
        if(this.filtered_count==0) return this.getFilteredTrips(); 
         return this.filteredTripsComputed;
    }
    getFilteredTrips:function(){
      var resource = base_url+'report/transport/filter';
      var self = this;
      axios.post(resource, $.param({filters:this.filters})).then(function(response) {
        // here i'm performing some logics to make self.trips

        self.trips.push(data[trip]); //push data to self.trips

        self.filteredTrips = self.trips; // i need to return this array
      }).catch(function(response) {
            // error callback
        })
     }
}

Leave a comment