6

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 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this! This needs to be marked as the solution.
0

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

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

1 Comment

Hi, I've updated the above for clarification on my issue. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.