我在应用启动期间收到以下错误:
Error: Nest can't resolve dependencies of the useFactory (?). Please verify whether [0] argument is available in the current context.
ConfigService
并将ConfigModule
作为第一个模块加载。不知道这是我的错,还是NestJS中的一个错误。
也许有人可以找到一些东西)谢谢。
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],
},
];
部分app.module.ts:
@Module({
imports: [
ConfigModule,
DatabaseModule,
GraphQLModule,
UsersModule,
],
exports: [DatabaseModule],
})
part config.module.ts:
@Module({
providers: [
{
provide: ConfigService,
useValue: new ConfigService(`./config`),
},
],
exports: [ConfigService],
})
答案 0 :(得分:1)
您必须在ConfigModule
内导入DatabaseModule
。默认情况下,Nest中的提供程序不是全局范围的。