Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to call admin.initializeApp at the top of each Cloud Function module file?

My Cloud Functions for Firebase index.js is getting to the size where I'd like to start splitting the code file off into multiple module files.

However it's unclear whether I need to call this line at the top of each module/file which will be accessing the Firebase database.

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

Please excuse me if this is really obvious. I am not an experienced Node.js user.

like image 894
Christopher R Avatar asked Mar 10 '23 08:03

Christopher R


1 Answers

For all of the code in all of your modules that want to make use of the Firebase Admin SDK, you will need to have an initialized admin object available. And in order to initialize the Admin SDK, you will need a functions object that comes from the following line:

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

So, you could do this at the top of all your module files:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

And that would provide an initialized admin object for you to use in the functions defined in that file. You can call the objects whatever names you want.

Alternately, you could figure out another way to get a single admin object into your code, but you have no obligation to do so. It's completely up to you.

like image 156
Doug Stevenson Avatar answered Apr 08 '23 22:04

Doug Stevenson