[英]NestJS. Custom provider, inject can't resolve dependencies of the useFactory
I get the following error during app start: Error: Nest can't resolve dependencies of the useFactory (?). Please verify whether [0] argument is available in the current context.
应用启动过程中出现以下错误:
Error: Nest can't resolve dependencies of the useFactory (?). Please verify whether [0] argument is available in the current context.
Error: Nest can't resolve dependencies of the useFactory (?). Please verify whether [0] argument is available in the current context.
ConfigService
is exported and ConfigModule
is loaded as first module. 导出
ConfigService
并将ConfigModule
加载为第一个模块。 Don't know if it's my fault or it's a bug in NestJS. 不知道这是我的错还是NestJS中的错误。 Maybe somebody could find something) Thank you.
也许有人可以找到一些东西)谢谢。
database.providers.ts: database.providers.ts:
import { Sequelize } from 'sequelize-typescript';
import { SEQUELIZE_TOKEN } from './constants';
import { User } from '../users/user.entity';
import { ConfigService } from '../config/config.service';
export const databaseProviders = [
{
provide: SEQUELIZE_TOKEN,
useFactory: async (configService: ConfigService) => {
const sequelize = new Sequelize({
dialect: 'mysql',
host: 'localhost',
port: 3306,
username: configService.databaseUser,
password: configService.databasePassword,
database: configService.databaseName,
operatorsAliases: false,
logging: false,
});
sequelize.addModels([User]);
await sequelize.sync({ force: process.env.NODE_ENV === 'local'
|| process.env.NODE_ENV === 'test',
});
return sequelize;
},
inject: [ConfigService],
},
];
Part app.module.ts: 零件app.module.ts:
@Module({
imports: [
ConfigModule,
DatabaseModule,
GraphQLModule,
UsersModule,
],
exports: [DatabaseModule],
})
Part config.module.ts: 零件config.module.ts:
@Module({
providers: [
{
provide: ConfigService,
useValue: new ConfigService(`./config`),
},
],
exports: [ConfigService],
})
You have to import ConfigModule
inside your DatabaseModule
. 你必须导入
ConfigModule
你里面DatabaseModule
。 The providers in Nest aren't globally scoped by default. 默认情况下,Nest中的提供程序不在全局范围内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.