I am switching over to the JavaScript web modular API from JavaScript namespaced (compat). My code is a Firebase Function with a http trigger running on GCP, so the code 'lives' in the GCP infra. My thought is that, because of this, I do not need to specify a bunch of data to initialise the App and Database, which would be the case if my code ran on ReactJS, or any other front end framework.
In my old code I simply initialised the app in a file called firebase-db
that was included wherever needed. This also gave me a handle to the database. The database handle is no longer needed with the modular API.
// This is the old namespaced code
const admin = require("firebase-admin");
const app = admin.initializeApp();
const database = admin.database(app);
module.exports = database;
In my new code I do not have this file and I am simply using the below code in my files.
const { ref, getDatabase, get } = require("firebase/database");
...
However, this does not work. I get an error that clearly states that the 'App' need to be initialised.
FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call initializeApp() first (app/no-app)
My new firebase-db
file looks like this; but it does not work:
// This is the new modular code
const { initializeApp } = require("firebase-admin");
const { getDatabase } = require("firebase/database");
const app = initializeApp(); //<-- This line generates an error!
const database = getDatabase(app);
module.exports = database;
The call initializeApp();
generates error:
TypeError: Cannot read property 'INTERNAL' of undefined
I have tied to look into the documentation, but cannot find any good examples on how to initialise the App so that I can get the Dabase handle using getDatabase().
https://firebase.google.com/docs/database/admin/retrieve-data#node.js_19 https://firebase.google.com/docs/database/web/read-and-write#web-modular-api_5 https://firebase.google.com/docs/reference/js/database.md#equalto_51c2c8b
Any help much appreciated! /K
The Firebase web SDKs aren't replacements for firebase-admin. On the backend, you still have to use firebase-admin and its normal API. There is no "modular" API for firebase-admin.
To be more specific: