Javascript firebase.database 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38248723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
firebase.database is not a function
提问by peteb
I am trying to upgrade from earlier firebase version to the latest in my ionic project. I followed thistutorial for upgrade. In step 4 from this page I am stuck on the last statement firebase.database().ref();
.
我正在尝试从较早的 firebase 版本升级到我的ionic 项目中的最新版本。我按照本教程进行升级。在此页面的第 4 步中,我停留在最后一条语句上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;
}
回答by peteb
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.
我在使用 Ionic 时遇到了这个问题,结果发现在使用最新的 Firebase 客户端时我没有包含所有内容。如果您已将 Firebase 包含为firebase-app
,则需要分别需要 Database 和 Auth 部分,因为以这种方式包含 Firebase 时它们不会捆绑在一起。
Add the following to your index.html
after you include firebase-app.js
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>
Obviously you don't need to use the CDN, you could use bower(probably the preferred way with Ionic) or NPMwith Browserify.
显然您不需要使用 CDN,您可以使用bower(可能是 Ionic 的首选方式)或带有 Browserify 的NPM。
// Browserify Setup
var firebase = require('firebase/app');
require('firebase/auth');
require('firebase/database');
Snippet below taken from the Firebase Web Setup Docs
下面的片段取自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-app - 核心 firebase 客户端(必需)。
firebase-auth - Firebase 身份验证(可选)。
firebase-database - Firebase 实时数据库(可选)。
firebase-storage - Firebase 存储(可选)。在 CDN 中,包含您需要的各个组件(首先包含 firebase-app)
回答by Ruan
A bit late to the party, but in case some one wanted to know the syntax in angular, (or Ionic 4) just add this to your .module.ts file (Note, as peterb mentioned, the /database import)
参加聚会有点晚了,但如果有人想知道 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: [
]
})
回答by Maksim Klimenko
Also faced this problem on @angular/firebase 5.1.2, solved when updated @angular/cli and all dependencies to the latest version.
在@angular/firebase 5.1.2 上也遇到了这个问题,当更新@angular/cli 和所有依赖项到最新版本时解决了。
回答by Mahmoud Ahmed Faragallah
I have the same error -firebase.database is not a function-but with different situationyou just need to add
我有同样的错误-firebase.database is not a function -但在不同的情况下你只需要添加
above and the link of javascript that contain the Firebase configuration.
上面和包含 Firebase 配置的 javascript 链接。
You may also try to use the deferAttribute in your script as it will not load the scripts until page elements are loaded.
您也可以尝试在脚本中使用defer属性,因为它在页面元素加载之前不会加载脚本。
回答by adolfosrs
First, make sure you are using
首先,请确保您正在使用
<script src="https://www.gstatic.com/firebasejs/3.1.0/firebase.js"></script>
Firebase authWithOAuthPopup
has changed a little bit in the new version.
FirebaseauthWithOAuthPopup
在新版本中发生了一些变化。
Now you don't use the ref to call authentication methods. You should be using firebase.auth()
insted.
现在您不使用 ref 来调用身份验证方法。你应该使用firebase.auth()
insted。
var auth = firebase.auth();
var provider = new firebase.auth.TwitterAuthProvider();
auth.signInWithPopup(provider).then(function(result) {
// User signed in!
var uid = result.user.uid;
}).catch(function(error) {
// An error occurred
});