Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with data in axios response

I'm new to axios.

In the past when I've made http requests I'm used to getting back an array/array of objects and this allows me to easily format the data how I want by using functions such as map and reduce. I then would render it to the DOM.

I've noticed in the response I get back is an observer object. How would I go about making the request so it gives me back an array? What is the standard for dealing with this observer object?

getSomething (myId) {
    return axios.get('/api/getSomething', {params: {'id': myId}})
                .then(response => console.log(response.data))
                .catch((promise) => this.handleError(promise));
}

Thanks

EDIT: Updated code. To clarify, when I call getSomething() response.data is an object even though I am sending it as an array on the backend. I am assuming that axios is changing this array to an object. The object has a bunch of extra properties like __ob__ and get 0

like image 249
Tom Headifen Avatar asked Oct 05 '17 22:10

Tom Headifen


1 Answers

So I found the issue. If you pass through an array where the keys are not in order e.g. [1: [], 5: [], 6:[]]. Javascript will change it into a observer object which has different properties in order to maintain the keys. This issue is not related to axios.

like image 60
Tom Headifen Avatar answered Oct 24 '22 17:10

Tom Headifen