首页 > 解决方案 > 使用打字稿的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

标签: typescriptfirebase

解决方案


如果你想同时调用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

推荐阅读