0

Whenever I call the cardslength variable or just cards.length, I get undefined outside of the function. Does this mean I need to use $scope? I am using angular 1.0.5 btw because this is part of a tutorial I am following and I needed to use that version.

This is my code:

eventsApp.factory('eventData', function ($resource, $q) {
var resource = $resource('/data/event/:id', {id: '@id'});


//////////////////////This is the part that doesn't seem to be working//////////
var cardslength
var cards = resource.query(function() {
        cardslength = cards.length;
        });
        console.log(cardslength);
        console.log(cards.length);
////////////////////////////////////////////////////////////////////////////////


return {
    getEvent: function () {
        var deferred = $q.defer();
        resource.get({id: 1},
            function (event) {
                deferred.resolve(event);
            },
            function (response) {
                deferred.reject(response);
            });

        return deferred.promise;
    },
    save: function(event) {
        var deferred = $q.defer();
        event.id = 1234;
        resource.save(event,
            function(response) { deferred.resolve(response);},
            function(response) { deferred.reject(response);}
        );
        return deferred.promise;
    }
};
});
7
  • 1
    That means resource.query() does not return anything, or your callback is execute before something is returned, overall bad service implementation Commented Aug 27, 2014 at 10:16
  • Inside the function it works fine Commented Aug 27, 2014 at 10:18
  • Before the function its just defined but has undefined, logical, where else do you access it and it doesn't work? also remember cardslength will have a value only when query() finishes execution Commented Aug 27, 2014 at 10:20
  • console.log(cardslength); console.log(cards.length); Are both not working when I request them after the function but they work when I put them inside of the resource.query function Commented Aug 27, 2014 at 10:25
  • Yes in that case its still undefined because .query() did not finish execution, so your console.log() is excuted before query() finishes. Commented Aug 27, 2014 at 10:26

1 Answer 1

1

Your console.log is executed before the .query() finishes. In order to avoid this, try as follows:

var cards = resource.query(function() {
    cardslength = cards.length;
}).then(function(){
    console.log(cardslength);
    console.log(cards.length);
});
Sign up to request clarification or add additional context in comments.

3 Comments

That also gets me undefined.
Actually, it's not working because you are using Angular 1.0.5. The above answer is only working with Angular 1.1.3 and later.
Acteally I just found out that in the latest Angularjs you need to use .$promise.then(){};

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.