firebase onAuth()抛出:TypeError无法读取未定义的属性"auth" [英] firebase onAuth() throwing: TypeError cannot read property 'auth' of undefined

查看:153
本文介绍了firebase onAuth()抛出:TypeError无法读取未定义的属性"auth"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Firebase用作主要的后端解决方案,并在整个应用程序中创建了一个用于处理auth的服务(因为我开始使用simpleLogin,现在我正尝试使用其内置的登录方法,所以我认为它可能最好先确定所有作用域,然后再从应用程序的其余部分访问它.

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方法.
但是监听方法onAuth()和offAuth()抛出此错误:

My firebase reference is working fine and so it is the auth methods.
But the listening methods onAuth() and offAuth() are throwing this error:

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

这是firebase本身的错误,还是有人看到我做错了什么? 尝试过使用有角度的版本并关闭其他Bower软件包,例如angularfire和firebase-simple-login,但到目前为止还没有成功.

Is this a bug with firebase itself or does anyone see something I'm doing wrong? 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.

我的身份验证服务代码如下,除了在.constant服务上定义我的网址外,我什么都没做.

My auth service code is as follows and I'm not doing anything beyond defining my url on a .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,
        ...
      }
    }

推荐答案

在JavaScript中,传递函数供以后调用时,请务必注意该函数将使用的 scope 被调用.例如,在定义闭包时,闭包将具有自己的范围,this可能不再指向我们的原始对象.使用fn.bind(...)解决了此问题.

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. When defining closures, for example, the closure will have its own scope and this may no longer point to our original object. Using fn.bind(...) addresses this issue.

这可能会很棘手,因为您正在使用的代码可能取决于特定的范围.诀窍是要确保在传递函数作为参数时范围不会改变,这可以通过在传递原始方法之前将方法明确 binding 绑定到原始对象来实现.

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.

查看 http://www.reactive .io/tips/2009/04/28/binding-scope-in​​-javascript/ http://alistapart.com/article/getoutbindingsituations ,以深入探讨该主题.

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.

这篇关于firebase onAuth()抛出:TypeError无法读取未定义的属性"auth"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆