javascript - 将firebase云功能拆分为不同文件的正确方法是什么?
问题描述
我正在尝试将大型云功能拆分为更小更易于维护的文件。但是,在部署时,我总是遇到同样的错误:
Error occurred while parsing your function triggers.
/Users/Raphi/Documents/Programing/Development/merchantAPI/functions/index.js:2
import { DirectPost } from '../functions/merchantPost';
^^^^^^
SyntaxError: Cannot use import statement outside a module
我昨晚大部分时间都在谷歌上搜索,但似乎没有任何文档
简化的 index.js:
import { DirectPost } from '../functions/merchantPost';
exports.portal = functions.https.onRequest(async (request, response) => {
var charge = new DirectPost()
})
简化的merchantPost.js:
export class DirectPost {
constructor(){}
}
解决方案
尝试在 Node.js 中包含来自另一个文件的 JavaScript 类定义。
还可以考虑为“门户处理程序”回调制作一个单独的文件,这样你就可以做到
exports.portal = functions.https.onRequest(portalHandler);
它使你的 index.js 更干净。
还可以考虑使用 TypeScript(其中导入的东西完全按照您在代码中尝试的方式完成),从长远来看,这将通过防止一些讨厌的错误来帮助您......
推荐阅读
- java - 任何方法重载都是编译时多态性吗?
- sql - 一个 Oracle SQL 查询中最多 2 个语句
- github - 为通过 GitHub Actions 执行的单元测试运行 localhost 服务器
- linux - 如何在不冒 OOM 杀手风险的情况下 mmap() 一个大文件?
- wordpress - 如何在 woocommerce 的新订单电子邮件中显示高级自定义字段数据
- unit-testing - KotlinTest 与 Koin:InvocationTargetException
- media - 如何从外部使用 sap-hybris media(image) url-或如何链接它
- java - Xtext 自定义警告抑制
- java - 用于检查邮箱中电子邮件的 Java 示例代码?
- macos - 我刚刚在我的 Mac 终端中输入 x = 'ls' 做了什么?