【发布时间】:2021-12-27 07:59:48
【问题描述】:
在使用 mongoose 连接到 mongodb 时,我的 nestjs 应用程序出现了奇怪的故障:
[08:08:26] Starting compilation in watch mode...
[08:08:31] Found 0 errors. Watching for file changes.
[Nest] 21652 - 16.11.2021, 08:08:35 LOG [NestFactory] Starting Nest application...
[Nest] 21652 - 16.11.2021, 08:08:35 LOG [InstanceLoader] MongooseModule dependencies initialized +61ms
[Nest] 21652 - 16.11.2021, 08:08:35 LOG [InstanceLoader] HttpModule dependencies initialized +1ms
[Nest] 21652 - 16.11.2021, 08:08:35 LOG [InstanceLoader] ConfigHostModule dependencies initialized +1ms
[Nest] 21652 - 16.11.2021, 08:08:35 LOG [InstanceLoader] AppModule dependencies initialized +1ms
[Nest] 21652 - 16.11.2021, 08:08:35 LOG [InstanceLoader] ConfigModule dependencies initialized +3ms
[Nest] 21652 - 16.11.2021, 08:08:35 LOG [InstanceLoader] ConfigModule dependencies initialized +1ms
[Nest] 21652 - 16.11.2021, 08:08:35 LOG [InstanceLoader] CacheModule dependencies initialized +27ms
[Nest] 21652 - 16.11.2021, 08:08:35 LOG [InstanceLoader] GraphQLSchemaBuilderModule dependencies initialized +1ms
[Nest] 21652 - 16.11.2021, 08:08:35 LOG [InstanceLoader] RedisCacheModule dependencies initialized +1ms
[Nest] 21652 - 16.11.2021, 08:08:35 LOG [InstanceLoader] GraphQLModule dependencies initialized +1ms
[Nest] 21652 - 16.11.2021, 08:09:05 ERROR [MongooseModule] Unable to connect to the database. Retrying (1)...
[Nest] 21652 - 16.11.2021, 08:09:38 ERROR [MongooseModule] Unable to connect to the database. Retrying (2)...
[Nest] 21652 - 16.11.2021, 08:10:11 ERROR [MongooseModule] Unable to connect to the database. Retrying (3)...
Mongo 在 docker 中运行:docker run --name my-mongo -p 27017:27017 -d mongo:latest。我可以通过 Robo 3T 和 mongosh shell 以及 Intellij DB 选项卡通过 'mongodb://127.0.0.1:27017' 或 'mongodb://localhost' 完美地达到它,但不能通过 Nest 应用程序。
这是我的 app.module.ts,为了清楚起见,连接字符串是硬编码的:
/** @format */
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { GraphQLModule } from '@nestjs/graphql';
import { AppService } from './app.service';
import { CompanyModule } from './company/company.module';
import { PersonModule } from './person/person.module';
import { MongooseModule } from '@nestjs/mongoose';
import { CallerModule } from './caller/caller.module';
import { RedisCacheModule } from './caching/redisCache.module';
import { ConfigModule } from '@nestjs/config';
@Module({
imports: [
RedisCacheModule,
GraphQLModule.forRoot({
autoSchemaFile: true,
playground: true,
sortSchema: true,
}),
// MongooseModule.forRoot('mongodb://' + process.env.MONGO_HOST),
MongooseModule.forRoot('mongodb://127.0.0.1:27017'),
CompanyModule,
PersonModule,
CallerModule,
ConfigModule.forRoot(),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
和环境:
[System Information]
OS Version : Windows 10
NodeJS Version : v14.17.2
NPM Version : 7.24.2
[Nest CLI]
Nest CLI Version : 8.1.4
[Nest Platform Information]
platform-express version : 8.1.2
schematics version : 8.0.4
mongoose version : 9.0.1
graphql version : 9.1.1
swagger version : 5.1.4
testing version : 8.1.2
common version : 8.1.2
config version : 1.0.3
axios version : 0.0.3
core version : 8.1.2
cli version : 8.1.4
此设置已在我的上一个项目中使用,它运行良好。此外,这段代码在我同事的机器上也能正常工作,他们甚至有相同的节点和 npm 版本(项目要求)。所以这对我来说似乎是一个问题,但话又说回来,自上次嵌套项目以来我没有改变任何东西。
- 连接mongo失败是什么原因?
- 如何调试/跟踪此错误?
感谢任何帮助!
【问题讨论】:
-
也许防火墙认为 Nest 不应该连接到 localhost?
-
不要这么认为,因为在我的旧项目中连接工作正常。