2
votes

I am using Firebase Hosting/Firestore/Functions for a Vue app. Hosting + Firestore are working fine. Now I wanna add more functionality and wanted to add Functions. To test locally, I initiated a Firestore and can run all 3 emulators. Now, when I start the emulators and goto the hosting emulator, I can insert a record, which also shows up in production database, but the function is not triggered. It's onCreate. I deployed the function and it works fine. I can see the console.log().

Why are local/production writes not triggering the cloud function?

Here is the function:

export const sendWelcomeEmail = functions.firestore.document('/activeJobsTestDB/{jobId}').onCreate((data,context) => {
  const inputRecord = data.data();
  console.log('here' + inputRecord);
  // const mailOptions = { 
  //   from: '"Spammy Corp." <[email protected]>',
  //   to: inputRecord.email,
  // };

});
i  Starting emulators: ["functions","firestore","hosting"]
⚠  Your requested "node" version "8" doesn't match your global version "12"
✔  functions: Emulator started at http://localhost:5001
i  firestore: Serving ALL traffic (including WebChannel) on http://localhost:8080
⚠  firestore: Support for WebChannel on a separate port (8081) is DEPRECATED and will go away soon. Please use port above instead.
i  firestore: Emulator logging to firestore-debug.log
✔  firestore: Emulator started at http://localhost:8080
i  firestore: For testing set FIRESTORE_EMULATOR_HOST=localhost:8080
i  hosting: Serving hosting files from: dist
✔  hosting: Local server: http://localhost:5000
✔  hosting: Emulator started at http://localhost:5000
i  functions: Watching "/Volumes/Work/playground/sforce-job/sforce-jobs/functions" for Cloud Functions...
>  here
✔  functions[sendWelcomeEmail]: firestore function initialized.
✔  All emulators started, it is now safe to connect.

I goto localhost:5000 and can use my app as I want, but the function is not triggered. Any help will be highly appreciated.

1
Are you trying to trigger a local cloud function with writes to production Cloud Firestore? That will not work. If you want to connect your local web app to the Firestore emulator, see this guide: firebase.google.com/docs/emulator-suite/…Sam Stern
Thanks @SamStern. Yes, I realised it will not work and I have to create a local mock database. I am currently creating a json which represents DocumentSnapshot and then pushing it via function shell to ensure it is working. Thanks anyway.ankittaneja
If you have more questions about how to use the emulators please file an issue on the firebase-tools GitHub repo!Sam Stern
@SamStern: Thanks man! No I don't have more issues. I am stubbing data myself.ankittaneja

1 Answers

0
votes

Answering myself here in case someone faces this: I realised there is no way to connect Cloud Firestore with local Cloud Function. Easy way to do it is to have a json which looks like DocumentSnapshot and then pass it to the function via shell. Use:

firebase function:shell
var data = require('./testData.json');
sendwelcomeEmail(data)

Other way will be to create a local database in firestore emulator, point the cloud function to look at firestore emulator. Then run hosting emulator and all 3 should play well nicely.