开发者问题收集

firebase.database 不是一个函数

2016-07-07
141079

我正在尝试将我的 ionic 项目 中的早期 firebase 版本升级到最新版本。我按照 教程进行升级。在本页面的第 4 步中,我卡在了最后一条语句 firebase.database().ref();

错误消息

TypeError: firebase.database is not a function

以下是我的代码。请帮忙。

...

// Initialize Firebase
this.config = {
    apiKey: "some-api-key",
    authDomain: "myapp.firebaseapp.com",
    databaseURL: "https://myapp.firebaseio.com",
    storageBucket: "project-somenumber.appspot.com",
};

...

this.authWithOAuthPopup = function(type) {
    var deferred = $q.defer();
    console.log(service.config);    // ---> Object {apiKey: "some-api-key", authDomain: "myapp.firebaseapp.com", databaseURL: "https://myapp.firebaseio.com", storageBucket: "project-somenumber.appspot.com"}
    firebase.initializeApp(service.config);
    console.log(firebase);  // ---> Object {SDK_VERSION: "3.0.5", INTERNAL: Object}
    service.rootRef = firebase.database().ref(); //new Firebase("https://rsb2.firebaseio.com"); ---> I am getting error on this line "TypeError: firebase.database is not a function"
    service.rootRef.authWithOAuthPopup(type, function(error, authData) {
        if (error) {
            service.authError = error;
            switch (error.code) {
                case "INVALID_EMAIL":
                    console.log("The specified user account email is invalid.");
                    break;
                case "INVALID_PASSWORD":
                    console.log("The specified user account password is incorrect.");
                    break;
                case "INVALID_USER":
                    console.log("The specified user account does not exist.");
                    break;
                default:
                    console.log("Error logging user in:", error);
            }
            deferred.resolve(service.authError);
        } else {
            service.authData = authData;
            console.log("Authenticated successfully with payload:", authData);
            deferred.resolve(service.authData);
        }
        return deferred.promise;
    });
    return deferred.promise;
}

var service = this;

更新

添加最新的数据库库后,这个问题解决了。

在此处更新我的代码

this.authWithOAuthPopup = function(type) {
    var deferred = $q.defer();
    console.log(service.config);
    firebase.initializeApp(service.config);
    console.log(firebase);
    service.rootRef = firebase.database(); //.ref(); //new Firebase("https://rsb2.firebaseio.com");

    var provider = new firebase.auth.FacebookAuthProvider();
    firebase.auth().signInWithRedirect(provider);
    firebase.auth().getRedirectResult().then(function(result) {
        if (result.credential) {
            // This gives you a Facebook Access Token. You can use it to access the Facebook API.
            var token = result.credential.accessToken;
            console.log(result);
            // ...
        }
        // The signed-in user info.
        var user = result.user;
    }).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // The email of the user's account used.
        var email = error.email;
        // The firebase.auth.AuthCredential type that was used.
        var credential = error.credential;
        // ...
    });
    return deferred.promise;
}
3个回答

我在使用 Ionic 时遇到了这个问题,结果发现我在使用最新的 Firebase 客户端时没有包含所有内容。如果您已将 Firebase 作为 firebase-app 包含在内,则需要单独要求 Database 和 Auth 部分,因为以这种方式包含 Firebase 时它们不会捆绑在一起。

在包含 firebase-app.js 后,将以下内容添加到您的 index.html

<script src="https://www.gstatic.com/firebasejs/3.1.0/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.1.0/firebase-database.js"></script>

显然您不需要使用 CDN,您可以将 bower (可能是 Ionic 的首选方式)或 NPM 与 Browserify 结合使用。

// Browserify Setup
var firebase = require('firebase/app');
require('firebase/auth');
require('firebase/database');

下面的代码片段取自 Firebase Web 设置文档

You can reduce the amount of code your app uses by just including the features you need. The individually installable components are:

firebase-app - The core firebase client (required).
firebase-auth - Firebase Authentication (optional).
firebase-database - The Firebase Realtime Database (optional).
firebase-storage - Firebase Storage (optional).

From the CDN, include the individual components you need (include firebase-app first)

peteb
2016-07-07

有点晚了,但如果有人想知道 angular 中的语法(或 Ionic 4),只需将其添加到您的 .module.ts 文件中(注意,正如 peterb 提到的,/database 导入)

import { AuthService } from './auth.service';
import { AngularFireAuthModule } from 'angularfire2/auth';
import { AngularFireDatabaseModule } from 'angularfire2/database';

@NgModule({
  imports: [
    AngularFireAuthModule,
    AngularFireDatabaseModule,
    AngularFireModule.initializeApp(environment.firebase),
  ],
  providers: [
  ]
})
Ruan
2018-12-05

我通过在构造函数中提供 url 解决了这个问题 firebase.database('https://123.firebaseio.com')

MarcoR
2020-11-26