因此,我遇到一个问题,当导出异步函数并导入该文件的其他非异步成员时,它们是未定义的。
设置方法如下:
functions.ts
export function getEnvironmentVariable(variable: string, defaultVal: string): string {
return process.env[variable] ?? defaultVal;
}
export async function handleDiscordToGameChat(member: GuildMember, chatChannel: string, content: string): Promise<{
ok: boolean;
response: string
}> {
const currentAuth = getAuthLvlFromMember(member);
const apiKey = getApiKeyForAuth(currentAuth);
if (!apiKey || currentAuth.rank < hsgRoleMap.GS.rank) {
return {
ok: false,
response: 'Insufficient permissions.'
};
}
const req = await fetch(`http://${API_ENDPOINT}/${isLocalServer() ? 'hsg-server' : 'hsg-rp'}/sendMessageToGame`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'token': apiKey
},
body: JSON.stringify({
content,
chatChannel,
adminDets: {
name: member.user.tag,
authLvl: currentAuth.acronym
}
})
});
const data = await req.json();
if (!data.ok) {
return {
ok: false,
response: data.response
};
}
return {
ok: true,
response: 'Successfully sent message.'
};
}
尝试通过以下方式访问另一个文件(getEnvironmentVariable
)中的导出成员config.ts
时:
import { getEnvironmentVariable } from './utils/functions';
console.log(typeof getEnvironmentVariable); // undefined
但是,在删除异步成员的导出后,它将返回函数。我应该将所有异步导出保存在单独的文件中还是这样?
编辑:忘记添加:我的IDE将导出的成员标识为一个函数,并且不会遇到任何编译错误,只是在运行时才有问题。
答案 0 :(得分:0)
这可能与您的导入语句的路径有关。可能是您的代码无法找到导出您的getEnvironmentVariable
函数的文件。