Nest 无法解析依赖项 LogService (?, LogFormatProvider)。请在 AppModule 上下文中提供索引 [0] 处的参数字符串
Nest can't resolve dependencies LogService (?, LogFormatProvider). Please argument String at index [0] is available in the AppModule context
My app.module.ts file looks like this:
import { Module, HttpModule } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { join } from 'path';
import { LaunchService } from './launch/launch.service';
import { LaunchResolver } from './launch/launch.resolver';
import { LaunchModule } from './launch/launch.module';
import { LogService } from '@fmr-pr103625/nest-scaffold';
@Module({
imports: [
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
typePaths: ['./**/*.graphql'],
definitions: { path: join(process.cwd(), 'src/graphql.ts') },
}),
HttpModule,
LaunchModule,
],
controllers: [AppController],
providers: [AppService, LaunchService, LaunchResolver, LogService],
})
export class AppModule {}
My app.service.ts file looks like this:
import { Injectable } from '@nestjs/common';
import { Logger, LogService } from '@fmr-pr103625/nest-scaffold';
@Injectable()
export class AppService {
@Logger()
private logger: LogService;
getHello(): string {
this.logger.error('Hello');
return 'Hello World!';
}
}
I am trying to use the in built logservice from another repo but this error is coming while running the file.
Nest can't resolve dependencies of the LogService (?, LogFormatProvider). Please make sure that the argument String at index [0] is available in the AppModule context.
Potential solutions:
- If String is a provider, is it part of the current AppModule?
- If String is exported from a separate @Module, is that module imported within AppModule?
@Module({
imports: [ /* the Module containing String */ ]
})
----------------------------****************** ****--------------------------------
让我知道我错过了什么,我该如何解决这个问题。
我只是检查简单的日志服务。
------------------------********************* ***--------------------------------
日志服务如下所示:
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogService = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@nestjs/common");
const winston = require("winston");
const log_level_1 = require("../constants/log-level");
const log_format_service_1 = require("./log-format.service");
const Console = winston.transports.Console;
let LogService = class LogService {
constructor(category, formatProvider) {
this.category = category;
this.formatProvider = formatProvider;
this.level = log_level_1.LogLevel.info;
this.logger = winston.createLogger({
level: this.level,
format: this.formatProvider.createFormat(this.category),
transports: [new Console()],
});
}
info(message, ...meta) {
this.log(log_level_1.LogLevel.info, message, ...meta);
}
error(message, ...meta) {
this.log(log_level_1.LogLevel.error, this.addingSplit(meta, message), ...meta);
}
warn(message, ...meta) {
this.log(log_level_1.LogLevel.warn, message, ...meta);
}
debug(message, ...meta) {
this.log(log_level_1.LogLevel.debug, message, ...meta);
}
log(level, message, ...meta) {
this.syncProps();
if (this.isLevelEnabled(level))
this.logger.log(level, message, ...meta);
}
isLevelEnabled(loggingLevel) {
return this.logger.levels[this.level] >= this.logger.levels[loggingLevel];
}
syncProps() {
this.logger.level = this.level;
}
addingSplit(meta, message) {
if (meta[0] instanceof Error && meta[0].message) {
message = message + ", errorMessage:";
}
return message;
}
};
tslib_1.__decorate([
config_core_1.Prop(),
tslib_1.__metadata("design:type", String)
], LogService.prototype, "level", void 0);
LogService = tslib_1.__decorate([
common_1.Injectable(),
tslib_1.__metadata("design:paramtypes", [String, log_format_service_1.LogFormatProvider])
], LogService);
exports.LogService = LogService;
----------------------------****************** *******--------------------------------
错误信息 Nest can't resolve dependencies LogService (?, LogFormatProvider). 告诉你你需要一个字符串作为第一个参数传递给 LogService 的构造函数的值。这将是 category 参数。在注入过程中,它不知道应该为该参数提供什么。通常,您会希望避免这样的构造函数设计,但如果需要,则需要考虑提供默认值。 (作为 Architect/Tech 主管,我通常会指示我的开发人员使用应用程序配置值来简化签名。)
让我们在单元测试环境中查看问题的原因,您应该可以复制并使用单元测试来帮助您解决问题。你会设置你的 testingModule 来为你提供注入支持,它可能看起来像这样:
testingModule = await Test.createTestingModule({
imports: [...],
providers: [LogService]
}).compile();
logService = testingModule.get(LogService);
注入器如何知道您要为 类别 参数提供什么?这就是 @Inject() 装饰器可以提供帮助的地方。在你的构造函数中,在你的 category 参数前面添加 @Inject('LOG_CATEGORY') 。这让注入器知道当它实例化 LogService 时你想通过名称“LOG_CATEGORY”提供一个值。
constructor(@Inject('LOG_CATEGORY') category: string, ...) {...}
现在让我们更改 testingModule 以提供一个值。在 providers 数组中,提供一个提供程序来指定您要使用的值。在下面的示例中,我想为类别提供值“默认”。
testingModule = await Test.createTestingModule({
imports: [...],
providers: [
LogService,
{
provide: 'LOG_CATEGORY',
useValue: 'Default'
}
]
}).compile();
logService = testingModule.get(LogService);
您选择如何解决问题取决于您,但我希望这能为您解决问题的一种方式提供一些启示。至少,它应该给你一个关于如何在单元测试中解决这种情况的例子。 :D
My app.module.ts file looks like this:
import { Module, HttpModule } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { join } from 'path';
import { LaunchService } from './launch/launch.service';
import { LaunchResolver } from './launch/launch.resolver';
import { LaunchModule } from './launch/launch.module';
import { LogService } from '@fmr-pr103625/nest-scaffold';
@Module({
imports: [
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
typePaths: ['./**/*.graphql'],
definitions: { path: join(process.cwd(), 'src/graphql.ts') },
}),
HttpModule,
LaunchModule,
],
controllers: [AppController],
providers: [AppService, LaunchService, LaunchResolver, LogService],
})
export class AppModule {}
My app.service.ts file looks like this:
import { Injectable } from '@nestjs/common';
import { Logger, LogService } from '@fmr-pr103625/nest-scaffold';
@Injectable()
export class AppService {
@Logger()
private logger: LogService;
getHello(): string {
this.logger.error('Hello');
return 'Hello World!';
}
}
I am trying to use the in built logservice from another repo but this error is coming while running the file.
Nest can't resolve dependencies of the LogService (?, LogFormatProvider). Please make sure that the argument String at index [0] is available in the AppModule context.
Potential solutions:
- If String is a provider, is it part of the current AppModule?
- If String is exported from a separate @Module, is that module imported within AppModule?
@Module({
imports: [ /* the Module containing String */ ]
})
----------------------------****************** ****--------------------------------
让我知道我错过了什么,我该如何解决这个问题。 我只是检查简单的日志服务。
------------------------********************* ***--------------------------------
日志服务如下所示:
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogService = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@nestjs/common");
const winston = require("winston");
const log_level_1 = require("../constants/log-level");
const log_format_service_1 = require("./log-format.service");
const Console = winston.transports.Console;
let LogService = class LogService {
constructor(category, formatProvider) {
this.category = category;
this.formatProvider = formatProvider;
this.level = log_level_1.LogLevel.info;
this.logger = winston.createLogger({
level: this.level,
format: this.formatProvider.createFormat(this.category),
transports: [new Console()],
});
}
info(message, ...meta) {
this.log(log_level_1.LogLevel.info, message, ...meta);
}
error(message, ...meta) {
this.log(log_level_1.LogLevel.error, this.addingSplit(meta, message), ...meta);
}
warn(message, ...meta) {
this.log(log_level_1.LogLevel.warn, message, ...meta);
}
debug(message, ...meta) {
this.log(log_level_1.LogLevel.debug, message, ...meta);
}
log(level, message, ...meta) {
this.syncProps();
if (this.isLevelEnabled(level))
this.logger.log(level, message, ...meta);
}
isLevelEnabled(loggingLevel) {
return this.logger.levels[this.level] >= this.logger.levels[loggingLevel];
}
syncProps() {
this.logger.level = this.level;
}
addingSplit(meta, message) {
if (meta[0] instanceof Error && meta[0].message) {
message = message + ", errorMessage:";
}
return message;
}
};
tslib_1.__decorate([
config_core_1.Prop(),
tslib_1.__metadata("design:type", String)
], LogService.prototype, "level", void 0);
LogService = tslib_1.__decorate([
common_1.Injectable(),
tslib_1.__metadata("design:paramtypes", [String, log_format_service_1.LogFormatProvider])
], LogService);
exports.LogService = LogService;
----------------------------****************** *******--------------------------------
错误信息 Nest can't resolve dependencies LogService (?, LogFormatProvider). 告诉你你需要一个字符串作为第一个参数传递给 LogService 的构造函数的值。这将是 category 参数。在注入过程中,它不知道应该为该参数提供什么。通常,您会希望避免这样的构造函数设计,但如果需要,则需要考虑提供默认值。 (作为 Architect/Tech 主管,我通常会指示我的开发人员使用应用程序配置值来简化签名。)
让我们在单元测试环境中查看问题的原因,您应该可以复制并使用单元测试来帮助您解决问题。你会设置你的 testingModule 来为你提供注入支持,它可能看起来像这样:
testingModule = await Test.createTestingModule({
imports: [...],
providers: [LogService]
}).compile();
logService = testingModule.get(LogService);
注入器如何知道您要为 类别 参数提供什么?这就是 @Inject() 装饰器可以提供帮助的地方。在你的构造函数中,在你的 category 参数前面添加 @Inject('LOG_CATEGORY') 。这让注入器知道当它实例化 LogService 时你想通过名称“LOG_CATEGORY”提供一个值。
constructor(@Inject('LOG_CATEGORY') category: string, ...) {...}
现在让我们更改 testingModule 以提供一个值。在 providers 数组中,提供一个提供程序来指定您要使用的值。在下面的示例中,我想为类别提供值“默认”。
testingModule = await Test.createTestingModule({
imports: [...],
providers: [
LogService,
{
provide: 'LOG_CATEGORY',
useValue: 'Default'
}
]
}).compile();
logService = testingModule.get(LogService);
您选择如何解决问题取决于您,但我希望这能为您解决问题的一种方式提供一些启示。至少,它应该给你一个关于如何在单元测试中解决这种情况的例子。 :D