Comments (5)
Finally got it working properly! The issue here was that I was initializing the app with initializeApp in multiple places. However, it gave no errors. I found this by running 'firebase serve' which produced the necessary errors. I moved initializeApp to my index.ts file and all of my functions now deploy. Thank you very much for your hard work.
Would this be the recommended place to initialize admin with this module?
from better-firebase-functions.
Finally got it working properly! The issue here was that I was initializing the app with initializeApp in multiple places. However, it gave no errors. I found this by running 'firebase serve' which produced the necessary errors. I moved initializeApp to my index.ts file and all of my functions now deploy. Thank you very much for your hard work.
Would this be the recommended place to initialize admin with this module?
I'm so glad you asked. I've been working on releasing the second part to a medium series that addresses this exact issue.
For the time being, I'll post a code snippet for you.
// Think of this module as the interface between Firebase SDKs and your app - All code to abstract away loading and initialising Firebase Libraries and services goes here. Your setup may vary.
import { firestore, initializeApp, apps, auth } from 'firebase-admin';
/**
* Internal
* Checks to see if Firebase App has been initialised (admin)
* @returns `boolean` false if more than one initialised, otherwise true
* @hidden
*/
const _notInit = (): boolean => apps.length === 0;
let db: FirebaseFirestore.Firestore;
let fAuth: auth.Auth;
/**
* Will ensure firebase app is initialised
* @returns `void`
*/
export const initAdmin = () => {
if (_notInit()) initializeApp();
};
/**
* Makes sure firebase app is initialised and returns Firestore object
*/
// tslint:disable-next-line: ban-comma-operator no-void-expression
export const getDb = (): FirebaseFirestore.Firestore => (initAdmin(), db || (db = firestore()));
/**
* Makes sure firebase app is initialised and returns Auth service
*/
// tslint:disable-next-line: ban-comma-operator no-void-expression
export const getAuth = (): auth.Auth => (initAdmin(), fAuth || (fAuth = auth()));
You're supposed to initialise firebase at the top of each module that uses it. If unit testing, you'll need an init statement at the top of each file. However, if you try initialise more than once, you'll get an error. I won't go into detail as to why this happens, but some people just put their initialise statement in a try-catch block, which is ugly. The above snippet checks if firebase has been initialised, will only initialise it once, caches the db or auth object, and returns the same instance wherever you may need it.
Simply import getDb or getAuth wherever you need them, and use at the top of each module to get your db or auth instance.
from better-firebase-functions.
Thank you, I have implemented this in my project.
What should be the average cold start time for a simple cloud function? I'm still seeing ~3secs/function which is not acceptable in my use case.
Is the cold start supposed to happen for every cloud function? Or should it only happen when the first function is called and then all other functions should run smoothly?
My use case is that I have built a multi-player online card game which has many callable functions per round and with the cold starts the first round is mostly just waiting on the various cloud functions to cold start. From the second round on, once all of the functions have started, it's smooth.
from better-firebase-functions.
thanks for the snippet, helped me sort out the single deploying function, however i later faced a issue where firestore throws
Exception from a finished function: Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.
is the snippet to only be used in index.ts or every module that uses those firebase functions? @george43g
from better-firebase-functions.
The above snippet is a module that you can import getDb and getAuth to reliably initialise and provide you with those services.
from better-firebase-functions.
Related Issues (20)
- Not finding functions HOT 2
- exportFunctions has an issue creating unique functions HOT 3
- Issue deploying functions in nested folders? HOT 1
- Javascript - firebase deploy just exits HOT 4
- Function in nested folder not working HOT 4
- Not sure on slow times HOT 1
- Is this repository unattended? Error generating function names within nested folders HOT 5
- not working when setting up multiple regions HOT 2
- does not work with firebase min instances HOT 2
- Library keeps creating deleted function HOT 3
- The automated release is failing 🚨 HOT 3
- The automated release is failing 🚨 HOT 5
- JS ESM Compatibility? HOT 3
- Firebase-functions v4 breaks better-firebase-functions: No functions are detected during deployment HOT 15
- Functions call singleton every invocation HOT 2
- Release Webpack plugin which works with better-firebase-functions to optimise function deployment
- test issue commenting by semantic release
- Unable to deploy v2 function with uppercase letter in function name HOT 10
- Suggestion: Link to your helpful medium article from the docs
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from better-firebase-functions.