question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Writing Multiple Functions

See original GitHub issue

I 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:

enter image description here

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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
samtsterncommented, Feb 10, 2020

@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!

1reaction
samtsterncommented, Feb 11, 2020

@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:

  1. Instead of globally loading all your dependencies and variables, only load them as needed within your functions.
  2. Instead of globally loading all of your function entrypoints, use the 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 your import or require 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing and Using Functions
You can have multiple functions per file but only the function whose name corresponds to the filename is accessible from outside the file....
Read more >
Write Functions with Multiple Parameters in Python
A function is a reusable block of code that performs a specific task. Learn how to write functions that can take multiple as...
Read more >
Function Overloading: Multiple Functions with the Same Name
Function Overloading: Multiple Functions with the Same Name · Key moments. View all · Key moments · Description · Key moments. View all...
Read more >
How to define multiple functions in c programming by Sanjay ...
Find Here: Links of All C language Video's PlaylistsC Interview Questions ...
Read more >
Multiple Functions in Single Program| Functions in MATLAB
Multiple Functions in Single Program| Functions in MATLAB | MATLAB Tutorial for Beginners In this video we are discussing Functions in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found