2

Previously, the function worked. but I added some code, now this error is coming. How to fix this issue?

Error: Error occurred while parsing your function triggers.

Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.
    at FirebaseAppError.FirebaseError [as constructor] (/Users/demo/functions/node_modules/firebase-admin/lib/utils/error.js:42:28)

code

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import { getLocations, getLocationsByType } from '../service/locationQuery/locationQuery';

//I tried both way, but same error came
// admin.initializeApp(functions.config().firebase);
admin.initializeApp();
exports.findUsers = functions.https.onCall(async (data: any, context: any) => {
1
  • 2
    why someone votes down my question? what is the reason? Commented Feb 17, 2020 at 12:24

4 Answers 4

13

I changed initialize place.. now It's working. I have no idea why is that.

import * as admin from 'firebase-admin';
admin.initializeApp();//add to here
import { getLocations, getLocationsByType } from '../service/locationQuery/locationQuery';

// admin.initializeApp(functions.config().firebase);

exports.findUsers = functions.https.onCall(async (data: any, context: any) => {
Sign up to request clarification or add additional context in comments.

2 Comments

It seems that you must have admin.initializeApp() before any functions are imported/exported in the index file. This is demonstrated in @Denis Alarie 4RP answer.
@Garrett This was the answer I was looking for! Thanks
4

Changing the place where you initialize your app might be important in ensuring that any declarations that require the app to be initialized come after. I had the same problem and when I made this change in order the problem went away..

THIS (succeeds)

import * as admin from 'firebase-admin';
admin.initializeApp();//add to here
import { getLocations, getLocationsByType } from 

is not the same as

THIS (fails)

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import { getLocations, getLocationsByType } from 
admin.initializeApp()

Comments

0

It is difficult to say where exactly is your problem (and I am not 100% sure the following will solve it), but there is a mismatch between the way you import the required modules:

import * as functions from 'firebase-functions';
import ...
//.....

and the way you export the Cloud Function:

exports.findUsers = functions.https.onCall(...)

As you will read here, the import corresponds to a TypeScript Cloud Function, while the export corresponds to a JavaScript one.


You should align your code to the correct mode (JS or TS) depending on what you use (probably TS, since data and context are typed):

JavaScript:

const functions = require('firebase-functions');

exports.findUsers = functions.https.onCall(async (data, context) => {...]);

TypeScript:

import * as functions from 'firebase-functions';

export const findUsers = functions.https.onCall(async (data: any, context: any) => {...]);

Comments

0

If you have modules files and have tried putting admin.initializeApp(); to no avail, then it's may be the file structure or mismatched imports

Check /functions/lib for updates after build, and if there is more than 1 functions/lib/**.js.map being generated

Check to see if you have both import admin = require('firebase-admin') and const admin = require('firebase-admin');

  1. delete lib directory

  2. Move your .ts .js script files from /functions/src/** and /functions/** to /functions/src/{folder}/** directory

  3. Covert all const admin = require('firebase-admin'); to import admin = require('firebase-admin')

  4. Rebuild & update the package.json main if need necessary

  5. firebase serve to test locally

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.