使用Google Cloud控制台时Cloud Functions的onCall方法失效
解决Firebase Cloud Functions v2 onCall处理器的TypeError问题
问题根源
你的代码出现Cannot read properties of undefined (reading 'on')错误,核心原因是模块系统语法冲突:package.json中声明了"type":"module"(ES模块规范),但代码里却用了CommonJS的require语法导入模块,导致firebase-functions模块加载异常。
修复方案(二选一)
方案1:改用ES模块的import语法
修改你的index.js,适配ES模块规范:
import { onCall, HttpsError } from "firebase-functions/v2/https"; export const helloHttp = onCall((request) => { return { data: request.data.name }; });
package.json保持"type":"module"不变。
方案2:移除ES模块声明,保留CommonJS语法
修改package.json,删除"type":"module"字段:
{ "dependencies": { "firebase-functions": "^5.0.0" } }
index.js的require写法无需改动。
额外测试注意事项
在GCP控制台测试Callable函数时,请求体必须符合Callable函数的格式要求,示例请求体如下:
{ "data": { "name": "测试名称" } }
内容的提问来源于stack exchange,提问作者Zino
相关产品推荐
相关产品推荐

