问题介绍
我有超过一百个Firebase云函数,为了保持代码的组织,我按照官方Firebase线程的指令将它们分成每个函数(例如,userFunctions
,adminFunctions
,authFunctions
,…)的单独文件。
在我的index.ts
中,我导入所有不同的函数文件为:
import * as adminFunctions from './modules/adminFunctions';
import * as userFunctions from './modules/userFunctions';
...
exports.adminFunctions = adminFunctions;
exports.userFunctions = userFunctions;
...
在我的userFunctions.ts
文件中,我将声明各个函数,其中一些函数将调用authFunctions.ts
中的其他可重用函数userFunctions.ts
import { https } from 'firebase-functions';
import { performAppCheckAuthentication } from './supportingFunctions/authFunctions';
exports.deleteExpiredOrganisationMembershipInvite = https.onCall(async (data, context) => {
// Perform AppCheck authentication
performAppCheckAuthentication(data, context)
// More code
...
})
交叉引用的authFunctions.ts
看起来像这样:
exports.performAppCheckAuthentication = function (
data: { [key: string]: any },
context: CallableContext
) {
return true; // There would be actual logic here in the real code
}
确切问题
当我让TypeScript尝试编译这段代码时,它在import语句中的userFunctions.ts
文件中给了我以下错误:
Module '"/supportingFunctions/authFunctions"'没有导出成员"performAppCheckAuthentication"。
我如何保持我的代码分割成不同的文件,以保持可维护性,但也绕过这个问题,不能导入函数?
您可能希望使用export语句而不是exports
全局:
export function performAppCheckAuthentication(
data: { [key: string]: any },
context: CallableContext
) {
return true; // There would be actual logic here in the real code
}
export const deleteExpiredOrganisationMembershipInvite = https.onCall(async (data, context) => {
// Perform AppCheck authentication
performAppCheckAuthentication(data, context)
// More code
...
})
文档