Double data array in response

16 views
Skip to first unread message

Dmitry

unread,
May 11, 2015, 3:52:17 AM5/11/15
to ang...@googlegroups.com
Hi
I'm using api which returns results like:

{
    "data":
    [
        {
            "contactId": 2,
            "uri": "a...@bb.cc",
            "favorite": true,
            "name": "John Surname"
        },
        {
           ......
        },
    ],
    "message": "200 OK"
}

But angular wraps result with data, so in the end I have do something like
result.data.data inside my function, like:

$scope.getSomething = function (value) {
                    return someService.getContacts(value).then(
                            function (result) {svn
                                return result.data.data; //????
                            }, function (error) {
                         ..........
                        return [];
                    });
                };

Is there a way to change the angular behavior, so I won't have 2 data arrays or maybe there are other ways to make it a bit cleaner.

Thank you in advance.

Sander Elias

unread,
May 12, 2015, 5:30:27 AM5/12/15
to ang...@googlegroups.com
Hi Dmitry,

Angulars $http returns a promise that will get resolved with the complete http response object:

The response object has these properties:

  • data – {string|Object} – The response body transformed witSo you don't h the transform functions.
  • status – {number} – HTTP status code of the response.
  • headers – {function([headerName])} – Header getter function.
  • config – {Object} – The configuration object that was used to generate the request.
  • statusText – {string} – HTTP status text of the response
So, you don't have double array's at all, it is just that the data you put in the data part is called data, wich might be a tad confusing :-)

Regards
Sander

Dmitry Shevchenko

unread,
May 12, 2015, 6:25:06 AM5/12/15
to ang...@googlegroups.com
Thank you Sander, I thought it is like that.

I will use data.data...

-- a bit of off-topic ---
What is the most common way in REST api to name the result?
- data
- payload
- response



--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/j7aL-4H9vMA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
Dmitry Shevchenko.

+47 47 970 332
dmit...@gmail.com
skype: dmitryshevchenko

Sander Elias

unread,
May 12, 2015, 7:37:59 AM5/12/15
to ang...@googlegroups.com
Hi Dmitry,

If you encapsulate your result in an extra layer you mean? Most of the times I just put in the payload without an additional wrapper, it gets wrapped anyway!
When it's easier in the server, to wrap your result set, any name is good. When this is happening I often see 'data', but I don't believe that there is a convention about that.

Regards
Sander
Reply all
Reply to author
Forward
0 new messages