firebase.database不是一个函数 [英] firebase.database is not a function

查看:389
本文介绍了firebase.database不是一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试从早先的firebase版本升级到我的离子项目中的最新版本。我遵循教程进行升级。在这个页面的第四步,我被困在最后一条语句 firebase.database()。ref();

错误消息

  TypeError:firebase.database不是函数

以下是我的代码。


$初始化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(); //新的Firebase(https://rsb2.firebaseio.com); --->我得到这行错误TypeError:firebase.database不是一个函数
service.rootRef.authWithOAuthPopup(类型,函数(错误,authData){$ b $如果(错误){
service.authError = error;
switch(error.code){
caseINVALID_EMAIL:
console.log(指定的用户帐号邮件无效);
break;
caseINVALID_PASSWORD:
console.log(指定的用户帐号密码不正确);
break;
caseINVALID_USER:
console.log(指定的用户帐户不存在);
break;
默认值:
console.log(Error logging user in:error,error);
}
deferred.resolve(service.authError);
} else {
service.authData = authData;
console.log(Authenticated successful 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(); //新的Firebase(https://rsb2.firebaseio.com);

var provider = new firebase.auth.FacebookAuthProvider();
firebase.auth()。signInWithRedirect(provider);
firebase.auth()。getRedirectResult()。then(function(result){
if(result.credential){
//这会给你一个Facebook访问令牌,你可以使用它访问Facebook API
var token = result.credential.accessToken;
console.log(result);
// ...
}
//已经登录的用户信息
var user = result.user;
))catch(function(error){
//处理错误
var errorCode = error .code;
var errorMessage = error.message;
//使用的用户帐户的电子邮件
var email = error.email;
// firebase.auth。使用的AuthCredential类型
var credential = error.credential;
// ...
});
return deferred.promise;
}


解决方案

事实证明,在使用最新的Firebase客户端时,我并没有包含任何内容。如果您已将Firebase作为 firebase-app 包含在内,则需要单独要求数据库和Auth作品,因为在以这种方式包含Firebase时,数据库和作品未捆绑在一起。

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

 < script src =https://www.gstatic.com/firebasejs/3.1 0.0 /火力-auth.js>< /脚本> 
< script src =https://www.gstatic.com/firebasejs/3.1.0/firebase-database.js>< / script>

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

  // Browserify设置
var firebase =要求( '火力/应用');
require('firebase / auth');
require('firebase / database');

摘自 Firebase Web设置文档 $ b


您可以减少应用程序的代码通过包括您需要的功能使用。可单独安装的组件包括:

firebase-app - 核心Firebase客户端(必需)。
firebase-auth - Firebase身份验证(可选)。

firebase-database - Firebase实时数据库(可选)。

firebase-storage - Firebase存储(可选)。

$ b $ p从CDN中,包含您需要的各个组件(包括firebase-app第一个)


I am trying to upgrade from earlier firebase version to the latest in my ionic project. I followed this tutorial for upgrade. In step 4 from this page I am stuck on the last statement firebase.database().ref();.

Error message

TypeError: firebase.database is not a function

Below is my code. Kindly help.

...

// 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;

Update

After adding latest database library this questions problem is solved.

Updating my code here

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;
}

解决方案

I ran into this with Ionic and it turned out that I wasn't including everything when using the latest Firebase Client. If you've included Firebase as firebase-app, then the Database and Auth pieces need to be required separately since they aren't bundled when including Firebase in this way.

Add the following to your index.html after you include 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>

Obviously you don't need to use the CDN, you could use bower (probably the preferred way with Ionic) or NPM with Browserify.

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

Snippet below taken from the Firebase Web Setup Docs

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)

这篇关于firebase.database不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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