为什么我的 Cloud Function 找不到我的入口点?

编程入门 行业动态 更新时间:2024-10-05 03:20:05

为什么我的 Cloud Function <a href=https://www.elefans.com/category/jswz/34/1771416.html style=找不到我的入口点?"/>

为什么我的 Cloud Function 找不到我的入口点?

我正在尝试使用打字稿和 ESM 部署 nodejs CloudEvent 云功能。我也在尝试自己在本地构建捆绑包,而不是让 gcp 为我做这件事。我知道这不是推荐的方法,但基于this.

似乎应该是可能的

我尝试了 2 种方法,都没有产生有效的 CloudEvent 云功能。

我的

index.ts
长这样:

import * as ff from '@google-cloud/functions-framework';

interface PubSubData {
  subscription: string;
  message: {
    messageId: string;
    publishTime: string;
    data: string;
    attributes?: {[key: string]: string};
  };
}

ff.cloudEvent<PubSubData>('myFnName', ce => {
  console.log(JSON.stringify(ce));
});

该代码基本上是 this 示例...除了我必须用

import ff
替换
import * as ff
或云功能部署失败与
Provided module can't be loaded.  Is there a syntax error in your code?

要创建捆绑包,我使用

esbuild
这样的:
esbuild src/index.ts --bundle --platform=node --outdir=dist

输出一个

index.js
,我将其压缩并推入桶中。最后我用

部署了函数
gcloud functions deploy my-fn-name --source <my-bundle-bucket-url> --entry-point myFnName --gen2 --runtime nodejs16 --trigger-bucket <my-trigger-bucket-url> --trigger-location us

部署失败

Could not load the function, shutting down.
Did you specify the right function to execute?
Function 'myFnName' is not defined in the provided module.

所以我尝试完全不使用

@google-cloud/functions-framework
包,而是自己定义入口点函数

export const myFnName: (ce: PubSubData) => void = (ce: PubSubData) => {
    console.log(JSON.stringify(ce));
};

当我这样做时,函数部署成功,我可以将文件添加到我的触发器桶中,并在部署创建的 PubSub 订阅中看到消息弹出。非常令人兴奋。但是消息从未被确认,云功能也从不记录任何内容。

所以有两个问题: 关于为什么在我使用函数框架注册时云函数找不到我的入口点文件的任何想法? 当我不使用函数框架注册我的入口点函数时,为什么我的云函数忽略 PubSub 消息有什么想法吗?

回答如下:

更多推荐

为什么我的 Cloud Function 找不到我的入口点?

本文发布于:2024-05-31 00:12:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771076.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   入口   Cloud   Function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!