Search code examples
firebasegoogle-cloud-firestoregoogle-cloud-functionsfirebase-cli

Why doesn't firestore onWrite trigger get invoked on firebase cloud functions emulator?


I have a firestore with a collection called "chats"; I use the firestore emulator to insert a new document and I am expecting the onWrite trigger to get called while I am running index.js locally on my firebase cloud functions emulator (by running firebase emulators:start), but it never does.

I know that the emulator is connected to the right firestore emulator since I can read the data (see below), I just can't get the trigger to be invoked.

// My setup:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

// We are able to fetch a document correctly, meaning at least my
// cloud functions emulator is hooked up to the firestore emulator!
admin.firestore().collection("chats").doc("eApXKLLXA4X6tIJtYpOx").get()
  .then(doc => {
      if (doc.exists) {
         console.log("Document data:", doc.data()); // <- this works!!
      } else {
         throw new Error("No sender document!");
      }
   })

// This never gets called when I insert a new document through the emulator UI!
exports.myFunction = functions.firestore
  .document('chats/{chat-id}')
  .onWrite((change, context) => { console.log("on write") });

enter image description here


Solution

  • Try to change the document selector from chats/{chat-id} to chats/{chatId}. Looks like - symbol is forbidden here. If I use it, I get the following error in firebase-debug.log:

    [debug] [2020-06-01T12:17:29.924Z] Jun 01, 2020 3:17:29 PM com.google.cloud.datastore.emulator.impl.util.WrappedStreamObserver onError INFO: operation failed: Invalid pattern. Reason: [69:-] Expected '}' at end of capture expression.

    Another reason might be using a wrong project id, but it seems it's not your case. See: Firestore triggers of cloud functions in emulator are ignored