Javascript firebase.database不是一个函数
我正在尝试从早期的firebase版本升级到我的系统中的最新版本。我按照教程进行升级。在本页的步骤4中,我被最后一条语句Javascript firebase.database不是一个函数,javascript,angularjs,ionic-framework,firebase,firebase-realtime-database,Javascript,Angularjs,Ionic Framework,Firebase,Firebase Realtime Database,我正在尝试从早期的firebase版本升级到我的系统中的最新版本。我按照教程进行升级。在本页的步骤4中,我被最后一条语句firebase.database().ref()卡住了 错误消息 TypeError: firebase.database is not a function 下面是我的代码。请帮忙 ... // Initialize Firebase this.config = { apiKey: "some-api-key", authDomain: "myapp.f
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;
}
首先,确保您正在使用
<script src="https://www.gstatic.com/firebasejs/3.1.0/firebase.js"></script>
我在使用Ionic时遇到了这个问题,结果证明我在使用最新的Firebase客户端时没有包括所有内容。如果您已将Firebase作为Firebase app
包含,则需要单独使用数据库和身份验证,因为以这种方式包含Firebase时,它们不会捆绑在一起
在包含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>
下面的片段摘自
您可以通过只包含所需功能来减少应用程序使用的代码量。可单独安装的组件包括:
firebase应用程序-核心firebase客户端(必需)。
firebase身份验证-firebase身份验证(可选)。
firebase数据库-firebase实时数据库(可选)。
firebase存储-firebase存储(可选)
从CDN中,包括您需要的单个组件(首先包括firebase应用程序)
聚会有点晚了,但如果有人想知道angular(或Ionic 4)中的语法,只需将其添加到.module.ts文件中(注意,正如peterb所提到的,/database import)
在@angular/firebase 5.1.2上也遇到了这个问题,在将@angular/cli和所有依赖项更新到最新版本时得到了解决。我有相同的错误-firebase。数据库不是一个函数-,但情况不同您只需添加
以及包含Firebase配置的javascript链接
您也可以尝试在脚本中使用“延迟”属性,因为在页面元素加载之前,该属性不会加载脚本。适用于在React native中遇到类似错误的用户(此。_database.native.on不是函数)-
在添加firebase新服务(数据库/身份验证…)后运行pod安装
终止metro bundler并使用“npx react native start”重新启动它
运行“npx反应本机运行ios”
这将创建一个新的构建,错误应该消失
Ref:我通过在构造函数中提供url解决了这个问题
firebase.database('https://123.firebaseio.com)使用
var firebase = require('firebase/app');
require('firebase/database');
您需要导入最新版本的firebase数据库。你可以在cndjs上找到建议对移动设备使用重定向方法。因此,我可以使用facebook使用重定向方法进行身份验证,但这会重新加载我的网页(目前正在chrome上测试ionic应用程序),我无法获得有效负载的详细信息。我已经更新了上面的代码,这太疯狂了,因为firebase()的官方文档目前没有显示链接,他们只是说,使用适当的库。如果查看CDN部分,firebase-database.js未列出,但这是实时数据库所需的。
// Browserify Setup
var firebase = require('firebase/app');
require('firebase/auth');
require('firebase/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: [
]
})
var firebase = require('firebase/app');
require('firebase/database');