2

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

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Zorayr
  • 23,770
  • 8
  • 136
  • 129
  • Make sure that firebase uses the correct project, see [Firestore triggers of cloud functions in emulator are ignored](https://github.com/firebase/firebase-tools/issues/2078) – Valeriy Katkov Jun 01 '20 at 07:32
  • I have setup my .firebaserc to point to the correct project. So this shouldn't be an issue ‍♂️ – Zorayr Jun 01 '20 at 07:41

1 Answers1

1

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

Valeriy Katkov
  • 33,616
  • 20
  • 100
  • 123
  • Yep, that did it, ‍♂️ I feel so silly, spent hours on this! – Zorayr Jun 01 '20 at 17:06
  • What about underscore? Is "_" forbidden as well? For example, Calls/{friend_id} – Axes Grinds Aug 16 '20 at 16:37
  • Is there a website the explains all the forbidden characters? I have underscores in many document names in cloud firestore functions and can't get them to trigger. – Axes Grinds Aug 16 '20 at 16:55
  • This, but when removing the weird nested `doc(\`${lhs}/{${lhs}Id}\`)`, I made it incorrectly `doc(\`${lhs}/id\`)`. Pub/sub is the service for triggers. So pay attention to emulator logs during start or in emulators ui. It will tell you pub/sub emulator is not running when it is called with errors. – Harry Scheuerle Apr 28 '21 at 23:23