将Firebase云功能拆分为不同文件的正确方法是什么? [英] What is the correct way to split firebase cloud functions into different files?

查看:61
本文介绍了将Firebase云功能拆分为不同文件的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将大型云功能拆分为较小的可维护性较小的文件.但是,在部署时,我总是会遇到相同的错误:

I'm trying to split a large cloud function into a smaller more maintainable files. However when deploying I always get the same error:

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

我昨晚大部分时间都在谷歌搜索,但是似乎没有任何文档

I spent most of last night googling but there doesn't seem to be any documentation for this

简化的index.js:

simplified index.js:

import { DirectPost } from '../functions/merchantPost';
exports.portal = functions.https.onRequest(async (request, response) => {
  var charge = new DirectPost()
})

简化的mergerPost.js:

simplified merchantPost.js:

export class DirectPost {
  constructor(){}
}

推荐答案

尝试在Node.js中包含来自另一个文件的JavaScript类定义.

还可以考虑为门户处理程序"回调创建一个单独的文件,以便您可以进行

Also consider making a separate file for the "portal handler" callback, so that you can do

exports.portal = functions.https.onRequest(portalHandler);

这使您的index.js更加整洁.

It makes your index.js much cleaner.

还可以考虑使用TypeScript(其中的导入操作完全按照您在代码中的尝试完成),从长远来看,它将通过防止一些讨厌的错误来帮助您...

Also consider using TypeScript (in which importing stuff is done exactly as you're trying to in your code) which will help you in the long run by preventing some nasty bugs...

这篇关于将Firebase云功能拆分为不同文件的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆