Firebase.Database Is Not a Function

firebase.database is not a function

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)

ErrorType: database is not a function Firebase

The only import you currently have for Firebase is:

import firebase from "firebase/app"

This imports only the basic SDK that allows you to initialize the firebase object. It does not import any product-specific SDKs.

To use the Realtime Database, you also need to import that SDK with:

import "firebase/database";

Why is firebase.database not a function?

In addition to importing Firebase App (the core Firebase SDK) with

<script src="https://www.gstatic.com/firebasejs/8.6.3/firebase-app.js"></script>

you need to add the imports for the Firebase products that you want to use. In your case it should be:

<script src="https://www.gstatic.com/firebasejs/8.6.3/firebase-database.js"></script>

See the doc for more details.

JS Firebase Database Error db.ref is not a function

You're mixing the new modular/v9 syntax of the API with the older namespaced syntax, and that won't work.

In v9 the equivalent of that last line is:

const dbref = ref(db, 'server/saving-data/fireblog');

Since you seem to be taking outdated code, I recommend keeping the documentation handy (for example, this section on getting a reference) to compare the v8 and v9 code samples, as well as reading the upgrade guide.

firebase.database() is not a function error in react

Since you are using the new Modular SDK, make sure you use it for all the Firebase services:

import "firebase/database"; // Older name-spaced version

The new Modular syntax for realtime database looks like:

import { getDatabase } from "firebase/database" 

let firebase = initializeApp(fbConfig);

const db = getDatabase(firebase)

function addSampleTodo () {
// Get a key for a new ToDo.
const newTodoKey = push(child(ref(db), 'posts')).key;

return update(ref(db), { updates['/todos/' + newTodoKey]: todoData });
}

You can find more details about it in the documentation. This is valid if you use the Firebase JS SDK v9+.


Alternatively, you can change the imports to compat version to keep using existing syntax.

import firebase from "firebase/compat/app"
import "firebase/compat/database"
// import "firebase/compat/[SERVICE_NAME]"

// Also use the name-spaced syntax everywhere else then
firebase.initializeApp(fbConfig);

const db = firebase.database() is not a function React native

You need to import firebase/database function into your project specifically. See Setup Step 2.

Your file should look like this

import firebase from "firebase/app"
import "firebase/database"

const firebaseConfig = {

....
}

firebase.initializeApp(firebaseConfig);

const db = firebase.database();

export default firebase;

Uncaught TypeError: database.ref is not a function using vue 3

You're mixing up Firestore and the Realtime Database here. While both databases are part of Firebase, they are completely separate and each has its own API.

As shown in the documentation on getting started with the Realtime Database and in the section on using the compat libraries, you get a database instance with:

import firebase from 'firebase/compat/app';
import store from "./store";
import 'firebase/compat/auth';
import 'firebase/compat/database'; // br>
// Remove all Firestore imports, and all fine-grained improts

And then

firebase.initializeApp(firebaseConfig);

// Get a reference to the database service
var database = firebase.database();


Related Topics



Leave a reply



Submit