Javascript 导入的typescript类';的实例化似乎不起作用
当我遇到这个场景时,我正在使用Typescript编写NodeJS服务器端代码。我创建了一个类Javascript 导入的typescript类';的实例化似乎不起作用,javascript,typescript,Javascript,Typescript,当我遇到这个场景时,我正在使用Typescript编写NodeJS服务器端代码。我创建了一个类Secret,为我提供环境变量集。给你 import Logger from './logger'; import dotenv from 'dotenv'; import * as fs from 'fs'; class Secret { private ENVIRONMENT: string; public constructor() { this.set
Secret
,为我提供环境变量集。给你
import Logger from './logger';
import dotenv from 'dotenv';
import * as fs from 'fs';
class Secret {
private ENVIRONMENT: string;
public constructor() {
this.setEnvironmentVaribales();
}
private setEnvironmentVaribales(): void {
if (fs.existsSync('.env')) {
Logger.logError('Using .env file to supply config environment variables');
dotenv.config({ path: '.env' });
} else {
Logger.debug('Using .env.mm file to supply config environment variables');
dotenv.config({ path: '.env.mm' });
}
this.ENVIRONMENT = process.env['NODE_ENV'];
}
public get environment(): string {
return this.ENVIRONMENT;
}
}
export default Secret;
然后我将Secret
导入到“Logger
,在那里我初始化了我的Logger。以下是logger.ts
import * as Winston from 'winston';
import Secret from './secret';
class Logger {
private logger: Winston.Logger;
private secret: string;
public constructor() {
this.secret = new Secret().environment;
console.log(this.secret);
this.initializeLogger();
}
/**
* Initializes the winston logger
*/
private initializeLogger(): void {
this.logger = Winston.createLogger({
transports: [
new (Winston.transports.Console)({
level: Secret.ENVIRONMENT === 'production' ? 'error' : 'debug'
}),
new (Winston.transports.File)({
filename: 'debug.log',
level: 'debug'
})
]
});
if (Secret.ENVIRONMENT !== 'production') {
this.logger.debug('Logging initialized at debug level');
}
}
}
export default new Logger();
问题是我没有在我的this.string
中获得预期的值。实际上,Secret
的新实例似乎没有生成。Iam将未定义作为记录器中环境
变量的结果。
我做错什么了吗。
对我来说唯一有效的方法似乎是当我更改我的秘密
以这样导出时。
import Logger from './logger';
import {dotenv} from 'dotenv/config';
import * as fs from 'fs';
if (fs.existsSync('.env')) {
Logger.logError('Using .env file to supply config environment variables');
dotenv.config({ path: '.env' });
} else {
Logger.debug('Using .env.mm file to supply config environment variables');
dotenv.config({ path: '.env.mm' });
}
export const ENVIRONMENT = process.env['NODE_ENV'];
或者是循环依赖阻止了它<代码>记录器
中的机密
和记录器
中的机密
。如果是这样,我甚至尝试从Secret
中删除Logger
import,并使用console.log()
。但是,仍然不能按我希望的方式工作。是的,这里有一个循环依赖关系:
- 在
logger.ts
中,导出的成员是logger
的实例:export default new logger()
- 在类
Logger
的构造函数中,类Secret
被实例化:newsecret().environment
- 在类
Secret
的构造函数中,调用成员setEnvironmentVaribales()
,该成员同步使用导出的Logger
实例
您当前的解决方案也无效:
if (fs.existsSync('.env')) {
Logger.logError('Using .env file to supply config environment variables');
dotenv.config({ path: '.env' });
} else {
Logger.debug('Using .env.mm file to supply config environment variables');
dotenv.config({ path: '.env.mm' });
}
export const ENVIRONMENT = process.env['NODE_ENV'];
在这里,Logger
在导出之前不能包含ENVIRONMENT
的内容
解决
最好的解决方案是不要从secret.ts
导入记录器,因为记录器需要首先完全执行此文件的内容
如果您真的需要交叉引用,那么如果您的类不从它们的构造函数中相互使用,那么它就会起作用
在任何情况下,如果日志记录器在执行其工作之前需要该秘密,则您不能要求它在提供该秘密之前工作。谢谢您指出这一点。我知道这可能是唯一的问题。但是在从Secret
中删除Logger
依赖项之后,我似乎用错误的命令测试了它。这就是为什么我最终得到了同样的结果。谢谢你抽出时间。