0

I wrote an angular service that is supposed to load customer details over http and store them for future use (in a singleton pattern).

However, when the http call returns successfuly, the variable into which I want to inject the data is undefined and an error is thrown.

Here's the code:

NBApp.service("serverDataService", ['$http', '$q', function ($http, $q) {
    var currentMemberDetails = [];

    this.currentMember = function (){
        if (this.currentMemberDetails.length>0)
            return this.currentMemberDetails[0];
        return null;
    };

    this.getMemberDetails = function (cardId) {
        //TBD - call members details web service
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: 'resources/customer.json'
        }).then
        (
            function success(response) {
                this.currentMemberDetails.push(response.data);
                deferred.resolve(data);
            },
            function failure(response) {
                deferred.reject(response);
            }
        );
        return deferred.promise;
    };
}]);

The currentMemberDetails is undefined, as shown by the chrome dev console.

1 Answer 1

1

Instead of

this.currentMemberDetails.push(response.data);

Use

currentMemberDetails.push(response.data);
Sign up to request clarification or add additional context in comments.

Comments

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.