Firebase onAuth () throwing: TypeError cannot read auth property of undefined

I am using Firebase as my main main solution and have created a service to work with auth throughout my application (since I started using simpleLogin and now I am trying to use their built-in login methods, I figured this might best do everything covered. before accessing it from the rest of the app).

The link to my base is working fine and so these are auth methods.
But the onAuth () and offAuth () listening methods throw this error:

Uncaught TypeError: Cannot read property 'auth' of undefined - firebase-debug.js: 9954

Is this a bug from Firebase itself or does anyone see something I'm doing wrong? Tried to play around with angular versions and disable other seating packages like angular fire and simple firebase-login but have no success so far.

My service service code looks like this and I am not doing anything other than defining my url in the .constant service.

    angular.module('myApp')
    .factory('auth', function (firebaseAPIUrl) {

      var ref = new Firebase(firebaseAPIUrl),
          onAuth = ref.onAuth,
          offAuth = ref.offAuth;

      onAuth(function (authData) {
        console.log(authData);
      });

      function getCurrentUser() {
        return ref.getAuth();
      }

      ...

      return {
        onAuth: onAuth,
        offAuth: offAuth,
        getCurrentUser: getCurrentUser,
        ...
      }
    }

      

+3


source to share


1 answer


In JavaScript, when passing a function to be called later, it is important to remember the scope from which the function will be called. For example, when defining closures, the closure will have its own volume, or this

may no longer point to our original object. Using it fn.bind(...)

fixes this problem.

This can seem daunting, as the code you are working with can be area-specific. The trick is to make sure the scope hasn't changed since you are passing the function as an argument, and this can be achieved by explicitly binding the method to the original object before passing it in.



Check out http://www.reactive.io/tips/2009/04/28/binding-scope-in-javascript/ and http://alistapart.com/article/getoutbindingsituations for deeper dives on this topic.

+5


source







All Articles