0
votes

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

There are some possibilities for organizing your Cloud Functions, but here [codeburst.io/… there is a nice post that goes trough the GitHub discussion that you mentioned and suggests a nice way to organize your cloud functions.Gonca
Thanks - I actually received a 404 error when visiting that pagetccpg288
Sorry, here it is: codeburst.io/…Gonca
Thanks I will review - help is appreciated!tccpg288