Writing Multiple Functions
See original GitHub issueI understand cloud functions were recently updated to 1.0. I saw the original post here and also created my own SO post, I am trying to understand how to create multiple cloud functions as I plan on having 5 - 10 in total and do not want to include all in my index.js:
I understand Cloud Functions recently updated to v1.0.
I am trying to write multiple functions from within Android Studio. I plan on having several cloud functions, and want to ensure my data structure is correct. Here is the current setup I have:
index.js
const functions = require('firebase-functions');
const trackVote = require('./trackVote')
const trendingCopy = require('./trendingCopy')
const admin = require('firebase-admin');
admin.initializeApp();
exports.trackVote = functions.firestore.document('Polls/{pollId}/responses/{userId}').onCreate(trackVoteModule.handler);
exports.trendingCopy = functions.firestore.document('Polls').onCreate(trendingCopyModule.handler);
trackVote:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.handler = (change, context => {
const data = change.after.data();
const answerSelected = data.answer;
const answerRef = admin.firestore().doc(`Polls/${event.params.pollId}/answers/${answerSelected}`);
const voteCountRef = admin.firestore.doc(`Polls/${event.params.pollId}/vote_count`);
return admin.firestore().runTransaction(t => {
return t.get(answerRef)
.then(doc => {
if (doc.data()) {
t.update(answerRef, { vote_count: doc.data().vote_count + 1 });
}
})
};
//TODO DO NOT ADD TO GIT
return admin.firestore().runTransaction(t => {
return t.get(voteCountRef)
.then(doc =>){
if (doc.data()){
t.update(voteCountRef, {vote_count:doc.data().vote_count+1});
}
}
});
});
});
Below is my console:
Error: functions predeploy error: Command terminated with non-zero exit code1
EDIT: I have seen this as a proposed solution, however it provides multiple options and unsure of best practice: https://github.com/firebase/functions-samples/issues/170
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:10 (8 by maintainers)
Top GitHub Comments
@cdock1029 we took your suggestion and added a new page on this topic: https://firebase.google.com/docs/functions/organize-functions
In our opinion something like
better-firebase-functions
is premature optimization in most cases, although there’s nothing wrong with using it!@cdock1029 you can check out this article for some benchmarks but I want to point out that there are two optimizations in better-firebase-functions:
GCLOUD_FUNCTION
environment variable to decide at runtime which one you need to execute and only evaluate that code.(1) can be a big help because dependencies can be very large, this is something you can do yourself without anything as complex as
better-firebase-functions
. You just move yourimport
orrequire
statements into the function body.(2) only matters if your codebase, excluding dependencies, is so big that even evaluating the surface of your JS code is expensive. It’s extremely unlikely that this is your bottleneck, you’d probably need tens of thousands of lines of JS to even notice the benefit of optimizing for this.