typescript - 使用打字稿的firebase功能组织
问题描述
我正在为我的 firebase 功能使用打字稿,并且有点卡在代码组织上。此时我不需要移动触发函数,并且可以将它们保存在 index.ts 文件中。我需要的只是帮助将其他实用程序功能远离 index.ts。
所以当前的 index.ts 是
import * as functions from 'firebase-functions';
import * as rp from "request-promise-native";
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase)
async function func1(){
return 'blaw'
}
async function func2(){
return 'blaw2'
}
module.exports = func1
module.exports = func2
index.ts 导入 { func1, func2 } from './comoncode'
export const on_plan_upgrade_request = functions.database.ref("plan_upgrades/{id}").onWrite(async (change, context) => {
console.log("start of on_feedback_received")
const rsp = await func1()
const rsp2 = await func2()
const command = change.after.val();
const data = {
from: from,
subject: "Plan upgrade request received",
html: "Hi " + command.name + "<br/>Thank you for your for your upgrade request.<br/><br/>We will get back to you shortly." +
"<br/><br/>Moblize.IT LLC<br/>http://moblize.it",
templateId: 'f9f96c8d-7515-40fb-ba34-787f0d34693e',
to: command.email,
cc: cc
}
sendEmail(data)
return ""
});
从上面的代码我想在另一个文件中使用 func1() 和 func2() 说 utility_code.ts
utility_code.ts 会是什么样子,以及如何在 index.ts 中引用它们。
更新:现在我得到错误 commoncode.ts is not a module
解决方案
如果你想同时调用func1()
和func2()
in utility_code.ts
,那么你需要导出它们:
export async function func1(){
return 'blaw'
}
export async function func2(){
return 'blaw2'
}
然后在 中utility_code.ts
,你可以导入它:
import { func1, func2} from "./utility_code"; //add your path
推荐阅读
- python - 如何以优雅的方式用 z3py 解开爱因斯坦之谜?
- flutter - 如何知道导航器中有多少视图?
- reactjs - Handle 改变formik中的对象。使用 setFieldValue
- excel - 格式错误导出到列表标签中的 excel
- node.js - 我们可以从 node js 应用程序或命令提示符获取电子版本的 vscode
- asp.net-core - 指定列时 ASP.NET DevExtreme Datagrid 中的 TypeLoadException
- angular - 复选框以角度的反应形式全部检查
- excel - Excel:A列中的最小值,而B列不为空
- java - GRPC重连并保持连接
- javascript - 将 resolve() 添加到 vuex 操作会中断组件中的数据加载