In the code snippet below, I have a simple factory that requests a json object containing location information. The requests are successful and the data is present in the object. However, there seems to be a scope problem as I am unable to access the properties of the locations object, even though I know that the object has the property and the correct array.
As indicated by the comments in the code snippet, I am facing an issue where I cannot access some values of the object when trying to output to the console.
Do you have any insights on what might be causing this problem?
.factory('Spots', function(){
return{
all: function($resource){
var locations;
var Locations = $resource('http://localhost\\:3000/locationsd/');
locations = Locations.get(function(){
console.log(locations.results[0].obj.name); // THIS WORKS -> gives me the name of the location
});
console.log(locations); // THIS WORKS -> log an object with a result array, etc.
console.log(locations.results[0].obj.name); // THIS DOESNT WORK -> TypeError: Cannot read property '0' of undefined
return locations;
}
}
})