如何导出名称包含连字符/破折号(短横线大小写)的 Firebase Cloud 函数 v2?

cbd*_*per 6 firebase google-cloud-functions

这是函数(helloWorld与您从函数命令获得的示例相同firebase init)。

import { onRequest } from 'firebase-functions/v2/https';

export const helloWorld = onRequest((req, res) => {
  res.send('Hello from Firebase v2!');
});
Run Code Online (Sandbox Code Playgroud)

当尝试部署它时,我收到以下错误:

错误:helloWorld v2 函数名称只能包含小写字母、数字、连字符,且长度不能超过 62 个字符

考虑到这一限制,我想将其命名为hello-world.

但是如何hello-world在 JS 中导出变量名呢?


更新

我刚刚尝试过:

exports['hello-world'] = onRequest((req, res) => {
  res.send('Hello from Firebase v2!');
});
Run Code Online (Sandbox Code Playgroud)

现在我收到错误:

错误:函数名称“hello-world”无效。函数名称不能包含破折号。

那我可以给它起什么名字呢?

我不想命名它helloworld(全部小写)。

他们接受连字符但不接受破折号?这非常令人困惑。


更新2:

我尝试了下面答案中的方法,但它似乎不起作用。

在此输入图像描述

在此输入图像描述

Mar*_*y B 2

有多种方法可以使用破折号导出函数。这是导出函数的一种方法:

import { onRequest } from 'firebase-functions/v2/https';

const helloWorld = onRequest((req, res) => {
  res.send('Hello from Firebase v2!');
});

export { helloWorld as "hello-world" }
Run Code Online (Sandbox Code Playgroud)

注意:如果您使用的是 Visual Studio 或其他 IDE,它可能会被识别为无效,但部署不会有问题。请参阅下面的示例:

在此输入图像描述

您还可以查看此文档以了解其他出口申报方式。