【问题标题】:Why doesn't firestore onWrite trigger get invoked on firebase cloud functions emulator?为什么不在 Firebase 云函数模拟器上调用 firestore onWrite 触发器?
【发布时间】:2020-09-19 11:12:14
【问题描述】:

我有一个名为“chats”的集合;我使用 firestore 模拟器插入一个新文档,并且我希望在我的 firebase cloud functions emulator 本地运行 index.js 时调用 onWrite 触发器(通过运行 firebase emulators:start),但它永远不会。

我知道模拟器已连接到正确的 Firestore 模拟器,因为我可以读取数据(见下文),但我无法调用触发器。

// 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") });

【问题讨论】:

标签: firebase google-cloud-firestore google-cloud-functions firebase-cli


【解决方案1】:

尝试将文档选择器从 chats/{chat-id} 更改为 chats/{chatId}。看起来- 符号在这里是被禁止的。如果我使用它,我会在firebase-debug.log 中收到以下错误:

[调试] [2020-06-01T12:17:29.924Z] 2020 年 6 月 1 日下午 3:17:29 com.google.cloud.datastore.emulator.impl.util.WrappedStreamObserver onError 信息:操作失败:模式无效。原因:[69:-] 捕获表达式末尾应为“}”。

另一个原因可能是使用了错误的项目 ID,但似乎不是您的情况。 见:Firestore triggers of cloud functions in emulator are ignored

【讨论】:

  • 是的,做到了,?‍♂️ 我觉得很傻,花了好几个小时!
  • 下划线呢? “_”也被禁止了吗?例如,通话/{friend_id}
  • 有没有解释所有禁用字符的网站?我在 Cloud Firestore 函数中的许多文档名称中都有下划线,并且无法触发它们。
  • 这个,但是当删除奇怪的嵌套doc(`${lhs}/{${lhs}Id}`)时,我弄错了doc(`${lhs}/id`)。 Pub/sub 是触发器的服务。因此,在启动期间或在模拟器 ui 中注意模拟器日志。当它被错误调用时,它会告诉你 pub/sub 模拟器没有运行。
猜你喜欢
  • 2021-02-13
  • 2018-10-11
  • 2020-08-24
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
  • 2020-10-30
  • 2018-06-01
  • 2017-08-02
相关资源
最近更新 更多