firebase.database不是一个函数

37 javascript angularjs firebase ionic-framework firebase-realtime-database

我正在尝试从早期的firebase版本升级到我的离子项目中的最新版本.我按照教程进行了升级.在这个页面的第4步中,我被困在最后一个声明中firebase.database().ref();.

错误信息

TypeError: firebase.database is not a function
Run Code Online (Sandbox Code Playgroud)

以下是我的代码.请帮助.

...

// 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;
Run Code Online (Sandbox Code Playgroud)

更新

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

在这里更新我的代码

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;
}
Run Code Online (Sandbox Code Playgroud)

pet*_*teb 57

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

index.html在您加入后添加以下内容firebase-app.js

<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>
Run Code Online (Sandbox Code Playgroud)

显然你不需要使用CDN,你可以使用bower(可能是Ionic的首选方式)或者使用Browserify的NPM.

// Browserify Setup
var firebase = require('firebase/app');
require('firebase/auth');
require('firebase/database');
Run Code Online (Sandbox Code Playgroud)

下面的代码段取自Firebase网络设置文档

您可以通过仅包含所需功能来减少应用使用的代码量.可单独安装的组件是:

firebase-app - 核心firebase客户端(必需).
firebase-auth - Firebase身份验证(可选).
firebase-database - Firebase实时数据库(可选).
firebase-storage - Firebase存储(可选).

从CDN,包括您需要的各个组件(首先包括firebase-app)

  • 这很疯狂,因为 firebase 的官方文档 (https://firebase.google.com/docs/web/setup) 目前没有显示 https://www.gstatic.com/firebasejs/6.1.0/ 的链接firebase-database.js 他们只是说,使用适当的库。如果您查看 CDN 部分,firebase-database.js 未列出,但这是实时数据库所需的。 (7认同)

Rua*_*uan 6

参加聚会有点晚了,但如果有人想知道 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: [
  ]
})
Run Code Online (Sandbox Code Playgroud)