0
 var createAttendances = function(){
    var stud = StudentResource.get(function(data){
        $scope.students = data.students;
        console.log($scope.students);
    });
    console.log(stud.students);
    console.log($scope.sudents);
};

inside resource get function it prints out Array of two objects (which is good) outside resource get it prints out undefined

it sees stud object but when i query to students params it returns undefined

as u can see main problem is to get $scope.students or data.students ouside StudentResouce.get func

2 Answers 2

2
StudentResource.get

is an async call, which means the lines below it can get executed even before the Resource GET call is completed, that is the reason why your variables are returning undefined outside the callback.

to access the data you fetched through GET call, you have to query it inside the call back itself.

Sign up to request clarification or add additional context in comments.

2 Comments

var stud = StudentResource.get(function(data){ return data.students; }); but i cannot query students property
it returns Resource {$promise: Promise, $resolved: false} but cannot query students param
0

This is an async call, so you can get result in this way, I got the same confusion in the beginning too.

StudentResource.get().$promise.then(function (result) {
       $scope.students = result.students;
        console.log($scope.students);
})

1 Comment

do a console.log('result: ' + JSON.stringify(result)); to check the result object

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.