0

I have a folder named functions, inside there is index.js. On index.js I have the main function that accept all http :

 exports.contentServer = functions.https.onRequest((request, response) => {

Since I have many more functions I need to organize my files, and I want to add another file, say payments.js, so that index.js can call functions inside payments.js, or even onCreate callbacks will be fired from payment.js.

If I just create another js file in functions, it won't work.

What has to be done to be able to have a single Cloud Function in another new js file ?

1 Answer 1

2

You can use a standard nodejs require to load code in other files.

index.js

const otherFunctions = require('./other.js');

exports.contentServer = functions.https.onRequest((request, response) => {
     otherFunctions.other()
}

other.js

exports.other = function() { ... }

I suggest reading the documentation for more information.

Sign up to request clarification or add additional context in comments.

4 Comments

then how do we call firestore inside otherFunctions? in generally it need admin initialize in index.js
Thank you! This worked with stripe API's and firebase functions
@NandaZ I think you can just pass in parameters such as functions, admin and e.t.c... to the function an use it as needed.
@PAGEBUNNII yes you are right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.