18

I have created a testing project and did configure everything according to documentation, but when I'm running the project, it's not working as expected, before writing this question, I did google and found some duplicate questions but the scenarios are different in each one, so I'm assuming this is not a duplicate question.

here are my terminal command and output:

functions ➤ npm run serve > functions@ serve /Users/codecrash/Developments/crm-firestore/functions > firebase serve --only functions ✔ functions: Using node@8 from host. ✔ functions: Emulator started at http://localhost:5000 i functions: Watching "/Users/codecrash/Developments/crm-firestore/functions" for Cloud Functions... i Your code has been provided a "firebase-admin" instance. i functions: HTTP trigger initialized at http://localhost:5000/demo-crm/us-central1/helloWorld Ignoring trigger "onWrite" because the Cloud Firestore emulator is not running. Ignoring trigger "onUpdate" because the Cloud Firestore emulator is not running. i functions: Beginning execution of "helloWorld" i Your code has been provided a "firebase-admin" instance. i functions: Finished "helloWorld" in ~1s 

I'm not sure, what's wrong. the helloWorld is working fine but not the onUpdate and onWrite.

I also tried by by running following command,

functions ➤ firebase emulators:start i Starting emulators: ["functions","firestore"] ✔ functions: Using node@8 from host. ✔ functions: Emulator started at http://localhost:5001 i firestore: Logging to firestore-debug.log ✔ firestore: Emulator started at http://127.0.0.1:8080 i firestore: For testing set FIREBASE_FIRESTORE_EMULATOR_ADDRESS=127.0.0.1:8080 i functions: Watching "/Users/codecrash/Developments/crm-firestore/functions" for Cloud Functions... i Your code has been provided a "firebase-admin" instance. i functions: HTTP trigger initialized at http://localhost:5001/demo-crm/us-central1/helloWorld i functions: Setting up Cloud Firestore trigger "onWrite" ✔ functions: Trigger "onWrite" has been acknowledged by the Cloud Firestore emulator. i functions: Setting up Cloud Firestore trigger "onUpdate" ✔ functions: Trigger "onUpdate" has been acknowledged by the Cloud Firestore emulator. i functions: Beginning execution of "helloWorld" i Your code has been provided a "firebase-admin" instance. i functions: Finished "helloWorld" in ~1s 

but still not luck with onUpdate or onWrite triggers. not sure what I'm doing wrong.

here is my code base:

const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp({}); exports.helloWorld = functions.https.onRequest((request, response) => { response.send("Hello from Firebase!"); }); exports.onWrite = functions.firestore.document('users/{userId}').onWrite((change, context) => { const document = change.after.exists ? change.after.data() : null; const oldDocument = change.before.data(); // perform desired operations ... console.log('local: onWrite change:', change); console.log('local: onWrite context:', context); console.log('local: onWrite document:', document); console.log('local: onWrite oldDocument:', oldDocument); return Promise.resolve(); }); exports.onUpdate = functions.firestore.document('users/{userId}').onUpdate((change, context) => { const document = change.after.exists ? change.after.data() : null; const oldDocument = change.before.data(); // perform desired operations ... console.log('local: onUpdate change:', change); console.log('local: onUpdate context:', context); console.log('local: onUpdate document:', document); console.log('local: onUpdate oldDocument:', oldDocument); return new Promise().then(() => { return Promise.resolve(); }).catch((error) => { return Promise.reject('error:code_crash'); }); }); 

I added keys in env variable:

GOOGLE_APPLICATION_CREDENTIALS="/Users/codecrash/Developments/crm-firestore/functions/certificate.json" FIREBASE_CONFIG="/Users/codecrash/Developments/crm-firestore/functions/certificate.json" 

Note: When I'm deploying it on cloud functions, it's working fine.

Thank you.

5
  • What version of the Firebase CLI are you using? Run firebase --version. Commented May 20, 2019 at 15:00
  • @DougStevenson it is 6.10.0 Commented May 20, 2019 at 18:11
  • Do you find any solution for this? I run the same issue here Commented Jul 13, 2019 at 8:40
  • same here, please help Commented Jul 13, 2019 at 17:45
  • Same here with firebase --version 7.1.0. It is not working also. Commented Jul 22, 2019 at 2:15

1 Answer 1

9

I don't know why this is not clearly mentioned in the documentation but it seems obvious that the client sdk needs to be made aware of the emulator. There is an API to point the client sdk to a local emulator instance. Here's how I set it up in my project right after I initialize firebase app. Hope that helps.

firebase.initializeApp(CONFIG); if (process.env.NODE_ENV === 'development') { firebase.functions().useFunctionsEmulator('http://localhost:5001'); } 

Update

When this question was asked (and answered), firebase local emulators were in the making. So the documentation was inadequate. Firebase now supports almost all the services (not storage) in what is being called as Firebase Emulator Suite and has great documentation. Check it out.

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

4 Comments

Let me try this solution. Thanks :)
Hi @ron4ex, I'm getting error firebase.functions() is not a function, in my case firebase is admin (npm firebase-admin module), any idea?
I'm not sure what you're trying to achieve. The code above is to trigger cloud functions locally from a client sdk. I suspect manual triggering w/o a client app is not supported: stackoverflow.com/questions/52416261/…
when I am adding this,if (process.env.NODE_ENV === 'development') { firebase.functions().useFunctionsEmulator('http://localhost:5001'); }, webpack throwing error default.a.functions is not a function. Any idea how to fix it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.