I have a Firebase Functions with Express app and I would like to use the Firestore instance on multiple controllers. So I created a simple service:
./services/firebase.serivce.ts
admin.initializeApp(functions.config().firebase);
const firestore = admin.firestore();
export const db = firestore;
Then in my controller
./controllers/test.ts
import { db } from './../services/firebase.service';
...
const collection = db.collection('contacts');
const router = express.Router();
router.post('/', (req, res) => {
...
and the app.js
import { TestController } from './controllers/test'
...
app.use('/contact', TestController);
export const api = functions.https.onRequest(app);
But Im getting the error:
The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.
And if I try to initializeApp()
in all my controllers, I get a message saying that the app already exists.
What can I do to share the same instance for multiple files?