1

I have a service which passes on the parameter pseudonym to the evironment. I call on this pseudonym in my views, but it doesn't appear at all.
How can I fix this to display the value in my views?

MyUser service:

app.service('MyUser', ['DatabaseRef', 'firebase', function(DatabaseRef, firebase) {
    var pseudonym ="";
    var userId = firebase.auth().currentUser.uid;

    return {
        pseudonym: function() {
            DatabaseRef.ref('/users/' + userId).once('value')
                .then(function (snapshot) {
                    pseudonym = snapshot.val().pseudonym;
                    console.log("pseudony: ", pseudonym);
                    return pseudonym;
                });

        }
    }
}]);

in my console, I see the value for the pseudonym. but not in my view html using {{pseudonym}}
and here is the example view controller:

app.controller('ExampleCtrl', ["MyUser",
    function (MyUser) {
     $scope.pseudonym = MyUser.pseudonym();
}]);
5
  • put a console.log and check if you can see the variable Commented Oct 15, 2016 at 11:16
  • @Sajeetharan I put a console log in my view controller and it gives me undefined! but it gives the right value in the console log inside the service. Commented Oct 15, 2016 at 11:38
  • Change the function name other than pseudonym Commented Oct 15, 2016 at 11:39
  • @Sajeetharan the function name in the service only? newpseudonym: function() {... for example? and in the view controller MyUser.newpseudonym(); ? Commented Oct 15, 2016 at 11:50
  • yeah, nothing pops up. Commented Oct 15, 2016 at 12:16

1 Answer 1

1

Return the promise created by the .then method:

app.service('MyUser', ['DatabaseRef', 'firebase', function(DatabaseRef, firebase) {
    //var pseudonym ="";
    var userId = firebase.auth().currentUser.uid;

    return {
        getUserName: function() {
            //return promise
            return (
                DatabaseRef.ref('/users/' + userId).once('value')
                    .then(function onSuccess(snapshot) {
                        let pseudonym = snapshot.val().pseudonym;
                        console.log("pseudony: ", pseudonym);
                        return pseudonym;
                })
            );
        }
    }
}]);

Then extract the value from that promise:

var app = angular.module('app', []);
app.controller('loginController',['$scope', 'MyUser',function($scope, MyUser)
{
    let promise = MyUser.getUserName();
    //Extract data from promise
    promise.then( function onSuccess(pseudonym) {
        $scope.pseudonym  = pseudonym;
        console.log($scope.pseudonym);
    });
}]);

The .then method of an object always returns a promise derived from the value (or promise) returned by the handler function furnished as an argument to that .then method.

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

Comments

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.