Functions-samples: Put each function in its own file

Created on 18 Mar 2017  路  3Comments  路  Source: firebase/functions-samples

Hi there,

How can I split my functions in its own files. I have tried to use require command, but it seems that I can't reimport resources like firebase-admin.

I am planning to write a lot of functions and put them all in index.js does not sound good.

Thanks in advance

Most helpful comment

index.js

var paymentFunctions = require('./payment_functions');

along with something like:

exports.paymentMethodTask = functions.database.ref('/newPaymentMethodTask/{taskId}').onWrite(event => {
    return paymentFunctions.processPaymentMethodTask(event);
});

With the folder structure:

/myProject/functions/index.js

/myProject/functions/payment_functions.js

Then export your functions in payment_functions.js as normal:

module.exports = {
    processPaymentMethodTask: function test(event) {
        //do something here with the event
    }
};

All 3 comments

I'm importing other .js files with firebase functions. Just think of the functions folder as root, I was mistakenly trying to import files from /functions parent folder.

index.js

var paymentFunctions = require('./payment_functions');

along with something like:

exports.paymentMethodTask = functions.database.ref('/newPaymentMethodTask/{taskId}').onWrite(event => {
    return paymentFunctions.processPaymentMethodTask(event);
});

With the folder structure:

/myProject/functions/index.js

/myProject/functions/payment_functions.js

Then export your functions in payment_functions.js as normal:

module.exports = {
    processPaymentMethodTask: function test(event) {
        //do something here with the event
    }
};

@ahaverty I changed my code as you suggested and it worked like a charm!

Thank you very much!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abhilash1in picture abhilash1in  路  4Comments

phuduong060196 picture phuduong060196  路  4Comments

IchordeDionysos picture IchordeDionysos  路  3Comments

tsaarikivi picture tsaarikivi  路  4Comments

beratuslu picture beratuslu  路  4Comments