gpt4 book ai didi

javascript - firebase.database 不是函数

转载 作者:IT王子 更新时间:2023-10-29 03:19:16 30 4
gpt4 key购买 nike

我正在尝试从早期的 firebase 版本升级到我的 ionic project 中的最新版本.我关注了this升级教程。在此页面的第 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;

更新

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

在这里更新我的代码

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

最佳答案

我在使用 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 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)

关于javascript - firebase.database 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38248723/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com