无法在谷歌云函数模拟器中触发发布订阅功能

时间:2021-04-08 12:59:03

标签: firebase google-cloud-firestore

我们正在尝试在发布 Firebase 函数之前创建一个开发环境。

我们已经成功安装并启动了一个本地模拟器,可以访问我们的 Firestore 数据库,可以调用 HTTPS 端点等等。开始 firebase emulators:start 节目:

firebase emulators:start --import=./testdata
i  emulators: Starting emulators: functions, firestore, hosting, pubsub
⚠  functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: auth, database
⚠  Your requested "node" version "12" doesn't match your global version "14"
i  firestore: Importing data from /home/forest/projects/icell/i_cell_parking_manager/firebase/testdata/firestore_export/firestore_export.overall_export_metadata
i  firestore: Firestore Emulator logging to firestore-debug.log
i  pubsub: Pub/Sub Emulator logging to pubsub-debug.log
i  hosting: Serving hosting files from: webserver
✔  hosting: Local server: http://localhost:5000
i  ui: Emulator UI logging to ui-debug.log
i  functions: Watching "..../firebase/functions" for Cloud Functions...
✔  functions[createParkingReservationsForNextWeek]: pubsub function initialized.
✔  functions[reserveSpaceForUser]: http function initialized (http://localhost:5001/i-cell-parking-manager-backend/us-central1/reserveSpaceForUser).

┌────────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! View status and logs at localhost:4000 │
└────────────────────────────────────────────────────────────────┘

┌───────────┬────────────────┬──────────────────────────┐
│ Emulator  │ Host:Port      │ View in Emulator UI      │
├───────────┼────────────────┼──────────────────────────┤
│ Functions │ localhost:5001 │ localhost:4000/functions │
├───────────┼────────────────┼──────────────────────────┤
│ Firestore │ localhost:8080 │ localhost:4000/firestore │
├───────────┼────────────────┼──────────────────────────┤
│ Hosting   │ localhost:5000 │ n/a                      │
├───────────┼────────────────┼──────────────────────────┤
│ Pub/Sub   │ localhost:8085 │ n/a                      │
└───────────┴────────────────┴──────────────────────────┘
  Other reserved ports: 4400, 4500

我们有一个名为 createParkingReservationsForNextWeek 的函数,它是一个定时的单个发布订阅,我们希望在不需要等待调度程序的情况下触发它。

 */
exports.createParkingReservationsForNextWeek = functions.pubsub
    .schedule(WEEKLY_NOTIFICATION_SCHEDULE)
    .timeZone(TIMEZONE_EUROPE_BUDAPEST)
....

根据https://firebase.google.com/docs/functions/local-emulator#web,我们可以通过在浏览器中导航到此 URL 来实现:http://localhost:5001/i-cell-parking-manager-backend/us-central1/createParkingReservationsForNextWeek

但是我们得到这个错误(也可以在模拟器日志中看到):

 functions: TypeError: Cannot read property 'data' of undefined
    at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:580:28
    at Generator.next (<anonymous>)
    at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:4:12)
    at processBackground (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:577:12)
    at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:673:23
    at Generator.next (<anonymous>)
    at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:8:71
    at new Promise (<anonymous>)
⚠  Your function was killed because it raised an unhandled error.

在日志中 (http://localhost:4000/logs):

 
14:50:31
I
function[createParkingReservationsForNextWeek]
Beginning execution of "createParkingReservationsForNextWeek"
14:50:31
W
function[createParkingReservationsForNextWeek]
TypeError: Cannot read property 'data' of undefined
    at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:580:28
    at Generator.next (<anonymous>)
    at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:4:12)
    at processBackground (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:577:12)
    at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:673:23
    at Generator.next (<anonymous>)
    at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:8:71
    at new Promise (<anonymous>)
14:50:31
W
function[createParkingReservationsForNextWeek]
Your function was killed because it raised an unhandled error.

Cloud-firestore-emulator 版本为 v1.11.9

1 个答案:

答案 0 :(得分:1)

您可以通过在 firebase shell 中按名称调用来在本地运行发布/订阅功能:

终端 1:firebase emulators:start

终端 2:firebase functions:shell 然后 createParkingReservationsForNextWeek()

如果您需要在调用时包含数据(可能是上述错误的原因),您也可以这样做:

// invokes a function with the JSON message { hello: 'world' } and attributes { foo: 'bar' }
myPubsubFunction({data: new Buffer('{"hello":"world"}'), attributes: {foo: 'bar'}})

有关详细信息,请参阅 Firebase documentation