简体   繁体   English

firebase onAuth()抛出:TypeError无法读取未定义的属性'auth'

[英]firebase onAuth() throwing: TypeError cannot read property 'auth' of undefined

I'm using Firebase as my main backend solution and have created a service for dealing with auth throughout the app (since I started to use simpleLogin and now I'm trying to use their built in login methods, I figured it might be better to just make all that scoped before accessing it from the rest of the application). 我正在使用Firebase作为我的主要后端解决方案,并创建了一个服务,用于在整个应用程序中处理auth(因为我开始使用simpleLogin,现在我正在尝试使用他们内置的登录方法,我认为它可能更好在从应用程序的其余部分访问它之前,只需创建所有范围)。

My firebase reference is working fine and so it is the auth methods. 我的firebase引用工作正常,因此它是auth方法。
But the listening methods onAuth() and offAuth() are throwing this error: 但是onAuth()和offAuth()的监听方法抛出了这个错误:

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

Is this a bug with firebase itself or does anyone see something I'm doing wrong? 这是firebase本身的错误还是有人看到我做错了什么? Tried to play around with angular versions and turning off other bower packages like angularfire and firebase-simple-login but have had no success so far. 试图玩角度版本和关闭其他凉亭包如angularfire和firebase-simple-login但到目前为止没有成功。

My auth service code is as follows and I'm not doing anything beyond defining my url on a .constant service. 我的auth服务代码如下所示,除了在.constant服务上定义我的url之外,我没有做任何事情。

    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,
        ...
      }
    }

In JavaScript, when passing around a function for later invocation, it is important to be mindful of the scope with which that function will be invoked. 在JavaScript中,当传递函数以便以后调用时,重要的是要注意调用该函数的作用域 When defining closures, for example, the closure will have its own scope and this may no longer point to our original object. 例如,在定义闭包时,闭包将有自己的作用域, this可能不再指向我们的原始对象。 Using fn.bind(...) addresses this issue. 使用fn.bind(...)解决了这个问题。

This can get tricky, as the code you're working with might depend on a particular scope. 这可能会变得棘手,因为您正在使用的代码可能取决于特定的范围。 The trick is to ensure that the scope hasn't changed as you're passing around the function as an argument, and this can be achieved by explicitly binding the method to the original object before passing it around. 诀窍是确保范围没有改变,因为你将函数作为参数传递,这可以通过在传递方法之前将方法显式绑定到原始对象来实现。

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. 查看http://www.reactive.io/tips/2009/04/28/binding-scope-in​​-javascript/http://alistapart.com/article/getoutbindingsituations ,了解有关此主题的更深入了解情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法读取未定义Vuejs的属性'$ auth' - TypeError: Cannot read property '$auth' of undefined Vuejs 类型错误:调用“firebase.auth().onAuthStateChanged((user)”时无法读取未定义的属性“auth” - TypeError: Cannot read property 'auth' of undefined when calling " firebase.auth().onAuthStateChanged((user)" 类型错误:无法读取未定义的属性“长度”- Firebase - TypeError: Cannot read property 'length' of undefined - Firebase Firebase TypeError:无法读取未定义的属性“ ac” - Firebase TypeError: Cannot read property 'ac' of undefined Firebase TypeError:无法读取未定义的属性“val” - Firebase TypeError: Cannot read property 'val' of undefined 类型错误:无法读取未定义的属性“凭据”-React-redux-firebase,Firebase 身份验证 - TypeError: Cannot read property 'credential' of undefined - React-redux-firebase, Firebase Auth firebase auth.js:12 未捕获的类型错误:无法读取未定义的属性“关闭” - firebase auth.js:12 Uncaught TypeError: Cannot read property 'close' of undefined TypeError:无法读取未定义的firebase和vuex的属性“ 0” - TypeError: Cannot read property '0' of undefined firebase and vuex FIREBASE警告:TypeError:无法读取未定义的属性“ then” - FIREBASE WARNING: TypeError: Cannot read property 'then' of undefined 调用firebase.auth()。onAuthStateChanged()时出现“ Uncaught TypeError:无法读取未定义的属性'resolve'” - “Uncaught TypeError: Cannot read property 'resolve' of undefined” when calling firebase.auth().onAuthStateChanged()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM