I encountered an issue with my api request that is included in a function within my Service script. Despite defining the variable "curruser" outside of the function to retain its value, I noticed that it becomes empty after exiting the script.
services.js
function fbUserInfo() {
ngFB.api({
path: '/me',
params: {
fields: '/*params*/'
}
}).then(
function(user) {
curruser = user;
$http.get(/*send GET request to my server*/).success(function(response) {
if (response.length < 20) {
curruser.firsttime = true;
} else {
curruser.firsttime = false;
}
console.log(curruser);
console.log("1");
});
},
function(error) {
alert('Facebook error: ' + error.error_description);
});
}
Although the console.log displays the correct JSON object retrieved from Facebook, when I attempt to return it in the statement:
return {
userInfo: function() {
fbUserInfo();
console.log(curruser);
return curruser;
}
it shows that curruser is an empty object. I made sure to include
var curruser;
at the beginning of the ".factory" as well.