Tom Headifen
Tom Headifen

Reputation: 1996

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

Upvotes: 6

Views: 4366

Answers (2)

Tom Headifen
Tom Headifen

Reputation: 1996

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.

Upvotes: 2

Luke Glazebrook
Luke Glazebrook

Reputation: 580

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

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

Upvotes: 0

Related Questions