Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration Problem with the new process.env.FIREBASE_CONFIG #216

Closed
ahaverty opened this issue Apr 5, 2018 · 6 comments
Closed

Migration Problem with the new process.env.FIREBASE_CONFIG #216

ahaverty opened this issue Apr 5, 2018 · 6 comments

Comments

@ahaverty
Copy link

ahaverty commented Apr 5, 2018

Version info

"firebase-admin": "5.11.0",
"firebase-functions": "1.0.0",
"firebase-tools": "3.18.0",

Test case

I've migrated from functions beta to v1, but I'm having issues initializing firebase-admin since migrating.
My setup was a main index.ts which called admin.initializeApp(functions.config().firebase); at the top of my index.ts and then exports each firebase function below.
Since the change from admin.initializeApp(functions.config().firebase); to the new admin.initializeApp(), I've been receiving the error:

Error: Can't determine Firebase Database URL.
    at FirebaseDatabaseError.Error (native)
    at FirebaseDatabaseError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
    at new FirebaseDatabaseError (/user_code/node_modules/firebase-admin/lib/utils/error.js:190:23)
    at DatabaseService.ensureUrl (/user_code/node_modules/firebase-admin/lib/database/database.js:75:15)
    at DatabaseService.getDatabase (/user_code/node_modules/firebase-admin/lib/database/database.js:52:26)
    at FirebaseApp.database (/user_code/node_modules/firebase-admin/lib/firebase-app.js:235:24)

I've determined that even attempting to console.log(process.env.FIREBASE_CONFIG) from the top of my index.ts prints undefined. However, printing the above from within each function export will print the config and allow me to initialize with admin.initializeApp().

It looks like I can workaround the change by adding admin.initializeApp() within each of my function exports, but I'd like to understand what's really going on. It's as if process.env.FIREBASE_CONFIG isn't set until each function is exported, whereas the beta version firebase.config().firebase was available instantly.
Is it recommended to initializeApp within each function?

@zgosalvez
Copy link

zgosalvez commented Apr 5, 2018

I have this similar issue. I have an index.ts which calls admin.initializeApp() and requires files that export functions.

Can you provide a specific example for the workaround? I tried adding admin.initializeApp() to one of my exported functions (files), but got this error.

Error: 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. But if you do want to initialize multiple apps, pass a second argument to initializeApp() to give each app a unique name.

@ahaverty
Copy link
Author

ahaverty commented Apr 5, 2018

@zgosalvez As a temporary workaround, I've wrapped admin.initializeApp() with a try catch.
I've got a library that I import in every firebase function, and I've added the initialize in there rather than repeatedly in every function.

@laurenzlong
Copy link
Contributor

Thanks for filing! This issue is fixed in firebase-tools 3.18.1, which was just released.

@zgosalvez
Copy link

As always, thank you @laurenzlong. I tried deploying using the latest firebase-tools version but failed. No code changes on my part. I filed a new issue firebase/firebase-tools#722 which is still related to admin.initializeApp().

@ahaverty
Copy link
Author

ahaverty commented Apr 6, 2018

The fix works for me @zgosalvez with a single initializeApp() without the try catch, all back to normal.

A fix + lib release in less than 22hrs since opening,
@laurenzlong can I send 🍩's to the Firebase HQ? ❤️

@zgosalvez
Copy link

@laurenzlong This is still an issue for me. Specifically, the error occurs at least in one of my HTTP trigger functions using express. Should I create a separate issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants