mgonto / restangular

AngularJS service to handle Rest API Restful Resources properly and easily
MIT License
7.87k stars 840 forks source link

Why object returned from POST contain extra data? #115

Closed se-panfilov closed 11 years ago

se-panfilov commented 11 years ago

Hi, i have a question about post queries. I am trying to send POST query to my backend, and catch the object in response. But returned object contain a lot of extra data. I just want get a pure object without anything else. Can you explain please what i doing wrong?

I'm do it like this:

$scope.result = Restangular.all("signup").post({
                email: "some@email.com",
                password: "somepassword"
            }).then(function (signup) {
               console.log(signup);
            }, function (response) {
                console.log("Error with status code", response.status);
            });

Backend answer me with object:

{email: "sdasd@scsacscccc.cs",
role: 2,
profile: "http://localhost:8080/profile/123/"}

I'm expected that returned object will displayed in console as is. But "signup" object contains much more fields, like:

addRestangularMethod: function () { [native code] }
all: function () { [native code] }
customDELETE: function () { [native code] }
customGET: function () { [native code] }
customGETLIST: function () { [native code] }
customOperation: function () { [native code] }
customPOST: function () { [native code] }
customPUT: function () { [native code] }
doDELETE: function () { [native code] }
doGET: function () { [native code] }
doGETLIST: function () { [native code] }
doPOST: function () { [native code] }
doPUT: function () { [native code] }
email: "some@email.com"
get: function () { [native code] }
getList: function () { [native code] }
head: function () { [native code] }
one: function () { [native code] }
options: function () { [native code] }
patch: function () { [native code] }
post: function () { [native code] }
put: function () { [native code] }
remove: function () { [native code] }
restangularCollection: false
route: "signup"
trace: function () { [native code] }
__proto__: g

There is my config:

angular.module('website.thirdpart.restqueries', [ 'restangular' ])
    .config(
        ['RestangularProvider', '$httpProvider', function (RestangularProvider, $httpProvider) {
            RestangularProvider.setBaseUrl('http://localhost\\:8080'); 
            RestangularProvider.setListTypeIsArray(false);
            RestangularProvider.setRestangularFields({
                id: "_id"
            });

            RestangularProvider.setResponseExtractor(function (response, operation, what, url) {
                if (operation === 'get') {
                    return response;
                } else if (operation === 'getList') {
                    return response.response[what].groups[0].items;
                }else{
                    return response;
                }
            });
        }]
    );

P.S. i doubt now, "signup" object in .then(function (signup) { - is the returned object, or object that was sended to rest? So, anyway, how can i get returned object after post query?

mgonto commented 11 years ago

Hey,

So, Restangular does add some functions and fields to every object you get, so that they're enhanced and you can actually keep on using Restangular for nested resources. That's part of the idea of using Restangular.

If you need the original object returned, you can do something like what's specified here https://github.com/mgonto/restangular/issues/100

Thanks!!

lucabelluccini commented 10 years ago

Restangular is a great library, but I think the choice of putting the returned object attributes at the same level of the "Restangular-related" methods is bad. The "workaround" is to copy (!!!) all the data.

piyushchauhan2011 commented 9 years ago

Restangular 1.4.0 above provides now method called plain() for getting plain response if needed.

screen shot 2015-07-18 at 11 28 18 am