I'm having trouble retrieving a Rest array and displaying it on my HTML page. The issue I am encountering is that in my factory, I can successfully fetch the array and display its contents, but in my controller, I always end up with an Undefined variable. Here is my Factory:
.factory('coursesService', function($resource) {
var service = {};
service.getAllCouses = function (){
var Courses = $resource("/myproject/rest/training/trainings");
var data = Courses.query().$promise.then(function(data)
{
service.data= data;
console.log("line 1: ", service.data[0].name);
console.log("line 1: ", service.data[0].creator);
console.log("line 2: ", data[1].name);
console.log("line 2: ", data[1].creator);
return service.data;
});
}
return service;
})
And here is my controller:
.controller("CoursesController",
function CoursesController($scope, coursesService) {
var courses = {};
courses = coursesService.getAllCouses();
console.log("courses: ", courses);
})
The output I am receiving looks like this:
courses: undefined
line 1: Angular
line 1: Object {id: "1", username: "User1", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f6a6c7a6d2e5f78727e7673317c7072">[email protected]</a>", password: "password", userProfile: Object}
line 2: JavaScript
line 2: Object {id: "1", username: "User1", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f481879186c5b49399959d98da979b99">[email protected]</a>", password: "password", userProfile: Object}
Why am I getting `courses: undefined`? Shouldn't it be displayed after the list that I'm showing in the factory?