1

i am trying to get the response from the server which returns something like true, false, or something else, depending on the request i do, the application i am developing is using a Restful server i created, and just for testing purposes i made it return true or false, later i will make it return proper json data.

here i am trying to get the response from this code (i am not sure how to use promises yet)

app.service('UserService', ['$resource', function($resource){
    var self = this;

    self.login = function(credentials){
        var handler = $resource('server/web/user/access', null, {
            access: {
                method: 'POST'
            }
        });

        handler.access(credentials).$promise.then(function(data){
            console.log(data);
        });
    }
}]);

for now i am just logging what it seems to return from the server, but the log shows another promise. for now i just need to get the data so i can make the conditions, parse objects, or show data it returns from the Restful server

1
  • I doubt the data is a promise, it should be a $resource instance. Perhaps you're mistaking it for a promise due to the $promise / $resolved properties it will have. See the bottom of the Usage / Returns section here ~ docs.angularjs.org/api/ngResource/service/$resource#usage Commented Nov 11, 2015 at 23:38

1 Answer 1

1

I'm guessing the problem here is that the $resource service expects either an object or array in the response and your simple Boolean value is not working correctly.

For now, you can use a response transformer to create a usable object

var self = this,
    handler = $resource('server/web/user/access', null, {
        access: {
            method: 'POST',
            transformResponse: function(val) {
                return {
                    val: val
                };
            }
        }
    });

self.login = function(credentials) {
    var promise = handler.access(credentials).$promise;
    promise.then(function(data) {
        $log.info(data.val);
    });
    return promise;
};
Sign up to request clarification or add additional context in comments.

2 Comments

thank you, i didn't know i would have to do that, i thought this was already assigned in a property
@nosthertus I didn't know either :)

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.