Firebase Cloud Functions 的 Firestore 触发器不起作用

Firestore triggers for Firebase Cloud Functions not working

目标 我的目标是使用 Cloud Functions

打印 Cloud Firestore 中任何 created/updated/deleted 文档的输出

设置 我目前正在使用 Firebase 模拟器进行测试。 Admin SDK 能够成功连接到 Firestore 数据库,但不会调用触发器。这是我的项目 firebase.json:

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "emulators": {
    "auth": {
      "port": 9099,
      "host": "127.0.0.1"
    },
    "firestore": {
      "port": 8080,
      "host": "127.0.0.1"
    },
    "ui": {
      "enabled": true
    }
  },
  "functions": {
    "source": "functions",
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint"
    ]
  }
}

所有触发器正在根据-

加载

Logs 显示以下设置:

Emulator UI logging to ui-debug.log
auth function initialized.
auth function initialized.
firestore function initialized.

┌─────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! It is now safe to connect your app. │
│ i  View Emulator UI at http://localhost:4000                │
└─────────────────────────────────────────────────────────────┘

┌────────────────┬────────────────┬─────────────────────────────────┐
│ Emulator       │ Host:Port      │ View in Emulator UI             │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Authentication │ 127.0.0.1:9099 │ http://localhost:4000/auth      │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Functions      │ localhost:5001 │ http://localhost:4000/functions │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Firestore      │ 127.0.0.1:8080 │ http://localhost:4000/firestore │
└────────────────┴────────────────┴─────────────────────────────────┘
  Emulator Hub running at localhost:4400
  Other reserved ports: 4500

index.js

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();

exports.newDoc = functions.firestore
  .document("/{collection}/{docID}")
  .onCreate((snapshot, context) => {
    console.log(snapshot.data());
  });

我试过的 添加任何新记录时,即使 Admin SDK 添加了它们,也不会读取文档。

事实证明,这实际上是 JavaScript 云函数模拟器的正确设置。我的环境出现问题,需要重新启动。

对于遇到相同问题的人,我会尝试相同类型的故障排除:

Try making the path use all wildcards to see if the path is the problem

确保它不是您当前环境的问题。