0

Just in case its important, this is how I am importing the functions stuff

import * as functions from "firebase-functions";
import admin = require("firebase-admin");

I am currently using the below code to check if the firebase function has already been initialized:

      if (admin.apps.length === 0) {
        admin.initializeApp();
      } else {
        functions.logger.log("App already initialized");
      }

I got this code from: How to check if a Firebase App is already initialized on Android

My problem is that this doesn't seem to be checking if the app has already been initialized because I see the log from the else statement but immediately after I see the error log that the default Firebase app does not exist.

If I call admin.initializeApp(); it works, but only sometimes, other times it would tell me that

"Unhandled error FirebaseAppError: The default Firebase app already exists. This means you called initializeApp() more than once without providing an app name as the second argument. In most cases you only need to call initializeApp() once"

What is the correct way to see if the app has already been initialized?

I am not sure if the below is important but here goes: I have my functions in separate files, to import the files into the index.ts file I:

  1. Import the function:
import checkPayment = require("./checkPayment");
  1. I'm not sure how to define what this is:
exports.checkPayment = checkPayment;

Perhaps I am importing the other functions incorrectly.

Thank you for your time.

Ricco D
  • 6,873
  • 1
  • 8
  • 18
  • If `admin.apps.length` is great than 0 there is an initialized app. If you're still getting an error after that check, please edit your question to show a [single snippet of code that any of us can run to reproduce the problem](http://stackoverflow.com/help/mcve) instead of describing the flow in words. – Frank van Puffelen Jul 04 '22 at 15:28

0 Answers0