The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services. at FirebaseAppError
Solution 1
You should just call admin.initializeApp()
at the global scope of your code. Don't pass any parameters to accept the default service account, which should have permission to read and write your Cloud Firestore database.
Solution 2
word of advice, sometimes this comes even if you initialized the app properly, at that point just check whether your db instance is placed inside the function not global.
#nodejs
var db = admin.database();//inside function
Solution 3
This might sound like crazy, But I had the same issue. Then I've changed my "admin.initializeApp();" place like below. Then it works.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const app = express();
//put here.
admin.initializeApp();
const db = admin.firestore();
const firebaseConfig = {
// config data
};
const firebase = require('firebase');
firebase.initializeApp(firebaseConfig);
Solution 4
you have to initialize app admin.initializeApp() before you use firestore or any other feature.
Solution 5
i am use typescript, a pattner singleton, because admin.initializeApp(); needs a only instance
import admin from 'firebase-admin';
export default class firebaseDAO {
private static _intance: firebaseDAO;
db: any;
private constructor() {
admin.initializeApp();
this.db = admin.firestore();
}
public static get instance() {
return this._intance || (this._intance = new this());
}
// iniciar un metodo
start() {
}
}
create a DAO
import firebaseDAO from '../database/firebase.dao';
constructor(){
this.db = firebaseDAO.instance.db;
}
async getPacientes() {
try {
const userQuerySnapshot = await this.db.collection('pacientes').get();
const users: any[] = [];
userQuerySnapshot.forEach(
(doc: any) => {
users.push({
id: doc.id,
data: doc.data()
});
}
);
this.response.resultado=users;
return this.response;
} catch (error) {
this.response.resultado=null;
return this.response;
}
}
Bob.B
Updated on February 12, 2022Comments
-
Bob.B about 2 years
I am learning to use Firebase Cloud Function and I am following Firebase's official tutorial video for Learn JavaScript Promises (Pt.1)
When I run a local server and go to the HTTP link I get
Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.
I tried finding the solution but couldn't find anywhere. I got some vaguely related question but for javascript and my code is in typescript. I did add the suggested syntax in both .js and .ts file
admin.initializeApp(functions.config().firebase);
But still, I get the same error
Here is my index.tcs.
import * as functions from 'firebase-functions'; import * as admin from 'firebase-admin'; // Start writing Firebase Functions // https://firebase.google.com/docs/functions/typescript export const getVoucher = functions.https.onRequest((request, response) => { const promise = admin.firestore().doc('/voucher/Eg2AtnGmKmX8Y0vBZiBG').get() const p2 = promise.then(snapshot=>{ const data = snapshot.data() response.send(data) }) p2.catch(error => { //Handlle the error console.log('error: '+error) response.status(500).send(error) }) });
The function works as intended when deployed to firebase tho.