Mizlul
Mizlul

Reputation: 2290

react native api call response

I am making an http call request and for some reason the response is abit strange!

My call looks like:

      getOne: id => {
            return axios.get('posts/1', {
                params: {
                    id: id
                }
            }).then(function (response) {
                return response;
            }).catch(function (error) {
                if (error.response) {
                    // The request was made and the server responded with a status code
                    return error;
                } else if (error.request) {
                    // The request was made but no response was received
                    return error.request;
                } else {
                    // Something happened in setting up the request that triggered an Error
                    return error.message;
                }
            });
        },

And from the component I call it as

categoryApi.getOne(1).then(response => {
            if (response.data) {
                this.setState({isLoading: false});
                console.log(response.data);
                this.setState({categories: this.state.categories.push(response.data)});
                console.log(this.state.categories);
            } else {
                this.setState({isLoading: false});
                Alert.alert(
                    'Something wrong happened!',
                    'My Alert Msg',
                    [],
                    {cancelable: true}
                )
            }
        });

And in console the response looks as:

     Object {
      "body": "quia et suscipit
       suscipit recusandae consequuntur expedita et cum
       reprehenderit molestiae ut ut quas totam
       nostrum rerum est autem sunt rem eveniet architecto",
       "id": 1,
       "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
       "userId": 1,
     }

The problem is that I want the response to be as an object like:

{
          "body": "quia et suscipit
           suscipit recusandae consequuntur expedita et cum
           reprehenderit molestiae ut ut quas totam
           nostrum rerum est autem sunt rem eveniet architecto",
           "id": 1,
           "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
           "userId": 1,
         }

What do I need to change so I dont have Object{} and only {}

Upvotes: 1

Views: 163

Answers (1)

nlloyd
nlloyd

Reputation: 2016

Try typing this in your JS console:

var myObject = {a: 1, b: 2}

Then inspect your new object:

myObject

It should show up as:

Object { a: 1, b: 2 }

Both {} and Object{} are the same thing. The latter is how the console chooses to display JavaScript Objects because they have other data associated with them that you can see if you click to expand the object:

{...} a: 1 b: 2 __proto__: Object {...}

Check out this tutorial for working with objects

Upvotes: 1

Related Questions