firebase.database is not a function

125,478

Solution 1

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)

Solution 2

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)

import { AuthService } from './auth.service';
import { AngularFireAuthModule } from 'angularfire2/auth';
import { AngularFireDatabaseModule } from 'angularfire2/database';

@NgModule({
  imports: [
    AngularFireAuthModule,
    AngularFireDatabaseModule,
    AngularFireModule.initializeApp(environment.firebase),
  ],
  providers: [
  ]
})

Solution 3

i solved this issue by giving the url in the constructor firebase.database('https://123.firebaseio.com')

Share:
125,478

Related videos on Youtube

Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    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;
    }
    
  • Admin
    Admin almost 8 years
    The doc recommends to use redirect method for mobile. So I am able to authenticate using the facebook using redirect method, but this reloads my webpage (currently testing ionic app on chrome) and I am not able to get the payload details. I have updated my the code above
  • raddevus
    raddevus almost 5 years
    It is crazy, because the official docs for firebase (firebase.google.com/docs/web/setup) do not currently show the link to gstatic.com/firebasejs/6.1.0/firebase-database.js They just say, use the appropriate libraries. If you look under the CDN section firebase-database.js is not listed but this is the one you need for the realtime db.