【发布时间】:2021-08-31 10:58:59
【问题描述】:
我正在尝试使用 Fastify-next 插件使用 Fastify 和 Next 初始化 Nest,在我集成 TypeOrm (MongoDB) 之前一切正常。当 Nest 加载 AppModule 时,它会抛出一个错误,指出在初始化 fastify-next 插件后,在 fastify 实例中找不到 .next() 函数。
代码
// main.ts
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter(),
);
await app.init();
const configService = app.get<ConfigService<IConfigTypes>>(ConfigService);
const appConfig = configService.get<IAppConfig>('app');
const fastify = await app.register(fastifyNext, {
dev: appConfig.isDev,
dir: appConfig.clientPath,
});
fastify.after(() => {
appConfig.staticRoutes.forEach((page) => fastify.next(page));
});
await app.listen(appConfig.port);
Logger.log(`???? Server is running on port ${appConfig.port}`, 'Bootstrap');
}
bootstrap();
// app.module.ts
@Module({
imports: [
ConfigModule.forRoot({
validate,
isGlobal: true,
cache: true,
load: [appConfig, shopifyConfig, databaseConfig],
}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService<IConfigTypes>) => {
const dbConfig = configService.get<IDatabaseConfig>('database');
const { type, host, base, user, pass } = dbConfig;
let url = 'mongodb+srv://';
url += `${user}:${pass}@${host}/${base}`;
url += '?retryWrites=true&w=majority';
return {
type,
url,
entities: [join(__dirname, '**/**.entity{.ts,.js}')],
synchronize: true,
useNewUrlParser: true,
logging: true,
};
},
inject: [ConfigService],
}),
],
})
export class AppModule {}
出现错误的控制台:
[8:45:52 AM] File change detected. Starting incremental compilation...
[8:45:52 AM] Found 0 errors. Watching for file changes.
[Nest] 26331 - 08/23/2021, 8:45:53 AM LOG [NestFactory] Starting Nest application...
[Nest] 26331 - 08/23/2021, 8:45:53 AM LOG [InstanceLoader] AppModule dependencies initialized +26ms
[Nest] 26331 - 08/23/2021, 8:45:53 AM LOG [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 26331 - 08/23/2021, 8:45:53 AM LOG [InstanceLoader] ConfigHostModule dependencies initialized +1ms
[Nest] 26331 - 08/23/2021, 8:45:53 AM LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 26331 - 08/23/2021, 8:45:53 AM LOG [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 26331 - 08/23/2021, 8:45:55 AM LOG [InstanceLoader] TypeOrmCoreModule dependencies initialized +2361ms
[Nest] 26331 - 08/23/2021, 8:45:55 AM LOG [NestApplication] Nest application successfully started +5ms
/dir/to/project/node_modules/avvio/boot.js:533
res = func()
^
TypeError: fastify.next is not a function
at /dir/to/project/src/server/main.ts:30:54 ===> appConfig.staticRoutes.forEach((page) => fastify.next(page));
我的临时解决方案是等到.next() 函数在fastify 实例中可用,然后运行代码。但我认为这不是解决这个问题的正确方法。
代码:
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter(),
);
await app.init();
const configService = app.get<ConfigService<IConfigTypes>>(ConfigService);
const appConfig = configService.get<IAppConfig>('app');
const fastify = await app.register(fastifyNext, {
dev: appConfig.isDev,
dir: appConfig.clientPath,
});
await new Promise((res) => {
const launch = async () => {
if (!fastify.next) {
setTimeout(launch, 200);
return;
}
appConfig.staticRoutes.forEach((page) => fastify.next(page));
await app.listen(appConfig.port);
Logger.log(`???? Server is running on port ${appConfig.port}`, 'Bootstrap');
res(null);
};
launch();
});
}
bootstrap();
如果我从AppModule 中删除TypeOrmModule,则错误消失,我认为该错误是由模块forRootAsync 的异步加载引起的。
有人知道如何解决它吗?如果您知道解决此问题的更好解决方案,请帮助我。谢谢!
【问题讨论】:
标签: javascript next.js nestjs typeorm fastify