Giter VIP home page Giter VIP logo

Comments (5)

zanesc avatar zanesc commented on August 17, 2024

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.

george43g avatar george43g commented on August 17, 2024

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.

zanesc avatar zanesc commented on August 17, 2024

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.

bkr32 avatar bkr32 commented on August 17, 2024

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.

george43g avatar george43g commented on August 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.