Dealing with data in axios response

Viewed 4000

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

2 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.

You can do something as simple as the following to access the data:

axios.get('/some/url').then(response => {
    console.log(response);
});
Related