3

I have a situation where when i console.log a object it shows to be null but in the same time to have some properties in it.

I'm fallowing up this tutorial or in short:

.factory('Auth', function ($http, $q, $firebase, FBURL, $location, $firebaseSimpleLogin, $rootScope) {
        var ref = new Firebase(FBURL);
        var auth = $firebaseSimpleLogin(ref);
        console.log(auth);
        var Auth = {
            signedIn: function () {
                return auth.user !== null;
            },
            login: function (user) {
                return auth.$login('password', user);
            }
        };

        $rootScope.signedIn = function () {
            return Auth.signedIn();
        };

        return Auth;
    });

and i get this:

enter image description here

in this case, after authenticating, console.log(Auth.signedIn()); return false because the object in the log shows as null, but when i open to look inside i can see the properties

what is this?

1
  • 1
    Is this process async? The user object is probably loaded by the time you go look at it in the console. Commented Sep 4, 2014 at 0:43

1 Answer 1

2

The $AngularFire.$fireBaseSimpleLogin.login function returns a promise. Meaning its asynchronous. Patrick Evan's comment is accurate. By the time you look at the object in the console, it now has information in it.

If you continue to follow the tutorial, towards the end the controller will wrap a login function to properly handle the promise.

Note: this is also why the Auth.login function that you've written simply returns the value of another function call. You're essentially creating an interface to abstract the login implementation details away from the controller (or other services) later. The controller doesn't really need to know what service you're using to login with, just that the login function returns a promise that it can do something with later.

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.