首页 > 解决方案 > 如何在 Firebase Functions shell 中运行 Pub-Sub 函数

问题描述

我无法在本地 Cloud Functions Shell 中运行我的 pub-sub 触发器。我通过以下方式创建了 Cloud Functions:

export const sendNotifications = functions.pubsub.schedule('0 10 * * *').timeZone('Asia/Kolkata').onRun(async (context) => {
    console.log('running sendNotificationForActivities');
    await sendMessages();
    return 0;
}

如此处所述,我尝试firebase functions:shell从我的系统运行,然后:

> sendNotifications({data: new Buffer('{"hello":"world"}'), attributes: {foo: 'bar'}})

或者

> sendNotifications({})

和其他变体。以下是我收到的错误堆栈跟踪:

firebase > sendNotificationForActivities({})
'Successfully invoked function.'
firebase > ⚠  TypeError: Cannot read property 'params' of undefined
    at cloudFunction (/Users/mayurdhurpate/code/pruoo_app/backend_admin/functions/node_modules/firebase-functions/lib/cloud-functions.js:109:38)
    at Run (/Users/mayurdhurpate/.npm-global/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:458:20)
    at /Users/mayurdhurpate/.npm-global/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:442:19
    at Generator.next (<anonymous>)
    at /Users/mayurdhurpate/.npm-global/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:7:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/mayurdhurpate/.npm-global/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:3:12)
    at Run (/Users/mayurdhurpate/.npm-global/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:435:12)
    at /Users/mayurdhurpate/.npm-global/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:457:15
    at Generator.next (<anonymous>)
⚠  Your function was killed because it raised an unhandled error.

我想我可能缺少必填params字段,但找不到输入参数的正确方法。我firebase-tools@7.1.1在 MacOS 上使用。

标签: firebasegoogle-cloud-platformgoogle-cloud-functionsgoogle-cloud-pubsubfirebase-cli

解决方案


我有同样的错误。该错误位于 cloud-functions.js:117:28 中。

即使上下文不存在,他也试图访问上下文的键。

在此处输入图像描述 我更新到最新版本,但错误仍然存​​在。

作为解决方法,我在 cloud-functions.js 的第 89 行添加了此代码:context = context || {};

在此处输入图像描述

这解决了错误,我可以使用 firebase shell。

我希望这个讨厌的黑客有帮助。直到谷歌修复他们的代码或更新文档。

多米尼克


推荐阅读