【问题标题】:Error connecting to firebase after upgrading angularfire升级 angularfire 后连接到 Firebase 时出错
【发布时间】:2016-07-23 04:42:29
【问题描述】:

自从升级 angularfire(到 2.0.1)以支持最新版本的 Firebase(3.2.0)后,我的应用程序不再连接到数据库。任何想法为什么?这是我以前工作的简单代码。

.constant('FIREBASE_URL', 'https://my_app_url.firebaseio.com/')

.factory('firebaseRef', function($firebase, FIREBASE_URL) {

      var firebaseRef = new Firebase(FIREBASE_URL);

      return firebaseRef;
    })

我的错误是:ionic.bundle.js:20434 ReferenceError: Firebase is not defined 错误引用了上面的行var firebaseRef = new Firebase(FIREBASE_URL);

我该如何解决这个问题?

【问题讨论】:

标签: angularjs firebase angularfire


【解决方案1】:

在 SDK 3 及更高版本中初始化 firebase 有所不同。

现在有一个全局 firebase(与 Firebase 构造函数相反),您需要先初始化它,然后才能获得对数据库根目录的引用。

所以你的代码可能看起来像这样:

ngModule.constant('FIREBASE_CONFIG', {
  apiKey: "apiKey",
  authDomain: "projectId.firebaseapp.com",
  databaseURL: "https://databaseName.firebaseio.com",
  storageBucket: "bucket.appspot.com",
});

ngModule.factory('firebaseRef', function($firebase, FIREBASE_CONFIG) {

  firebase.initializeApp(FIREBASE_CONFIG);
  return firebase.database().ref();
});

结帐the docs

【讨论】:

  • 谢谢,这里是超级愚蠢的问题。为什么要使用ngmodule. 或任何前缀?目前我的代码不使用名称前缀,因此 .constant 这就是让我感到困惑的部分,如果我使用 ngmodule. 那么我是否必须更改我所有的工厂和服务以使用 ngmodule. 作为前缀?
  • 我认为我的问题是命名/组织问题 :)
  • 它应该是对您使用 angular.module('your-module', []); 创建的现有 AngularJS 模块的引用。然后你可以像这样得到这个模块:var module = angular.module('your-module');
  • 感谢@idan ,在 app.js 中显示为 angular.module('yourAppsName', ['ionic', 'yourAppsName.services' ]),在 services.js 中显示为 angular.module('yourAppsName.services', []) 当我按照您的建议使用 yourAppsName 时,我仍然收到错误 yourAppsName is undefined。困惑。
  • var module = angular.module('yourAppsName') 应该可以工作,然后您可以像在答案中一样使用module
猜你喜欢
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-07
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
相关资源
最近更新 更多