I'm following the instructions on https://firebase.google.com/docs/functions/organize-functions to reorganize my cloud functions.
But I'm struggling to reuse some of the global const defined in the index.js file.
// index.js
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp()
const db = admin.firestore()
db.settings({ ignoreUndefinedProperties: true })
exports.choice = require('./choice')
...
// choice.js
const functions = require('firebase-functions')
exports.redirection = functions
.region('europe-west1')
.https.onRequest(async (req, res) => {
try {
const doc = await db.doc(`stbk${req.params[0]}`).get()
...
Got the following error: > ReferenceError: db is not defined
Any help would be greatly appreciated !
UPDATE
I've done the following:
// global.js
const admin = require('firebase-admin')
admin.initializeApp()
const db = admin.firestore()
db.settings({ ignoreUndefinedProperties: true })
module.exports = { admin, db }
// choice.js
const functions = require('firebase-functions')
const global = require('./global')
exports.redirection = functions
.region('europe-west1')
.https.onRequest(async (req, res) => {
try {
const doc = await global.db.doc(`stbk${req.params[0]}`).get()
Is it a valid solution?