Currently, I am exploring computed properties in Vue.js.
One of the computed methods I am working on involves making a request to an axios API to retrieve an array after applying some logic within the promise.
computed: {
filteredTrips: 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');
if(this.filtered_count==0) return this.getFilteredTrips(); //This is the axios method to get an array
return filtered;
}
},
methods: {
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
return self.trips; // i need to return this array
}).catch(function(response) {
// error callback
})
}
}
My challenge lies in the fact that getFilteredTrips()
does not return the self.trips
array. How can I achieve this using axios and JavaScript?