I have already read the answers to the question How do I return the response from an asynchronous call? But I'm not sure I understood it well and I think that my problem is a bit different. I change my service in this way:
.service('CommonService',['$firebase', function($firebase){
var username;
function onComplete(result){
username = result;
};
var getData = function(){
var ref = new Firebase("https://instafame.firebaseio.com");
ref.onAuth(function(authData){
var userid = authData.uid;
console.log(userid);
var newref = new Firebase("https://instafame.firebaseio.com/users/" + userid)
newref.once("value", function(snapshot) {
var data = snapshot.val()
newUsername = data.username;
callback(newUsername);
})
});
};
return{
getUsername: function(){
getData(onComplete);
return username;}
};
}])
In my controller I store inside a variable user
the return of the CommonService
:
var user = CommonService.getUsername();
console.log(user);
The problem is that the console still returns "undefined". I have tried to change the code following the suggestions but it doesn't run. What should I do?
Thanks in advance