无法使用 Mongoose(本地)从 Nest 连接到 MongoDB
Unable to connect to MongoDB from Nest using Mongoose (locally)
我的 nestjs 应用程序在使用 mongoose 连接到 mongodb 时出现奇怪的故障:
[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 以及通过 'mongodb://127.0.0.1:27017' 或 'mongodb://localhost' 使用 Intellij DB 选项卡完美地达到它,但不能通过嵌套应用程序。
这是我的 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 版本(项目要求)。所以这似乎是我这边的问题,但话又说回来,自从上一个 nest 项目以来我没有改变任何东西。
- 连接失败的原因是什么mongo?
- 如何debug/trace这个错误?
感谢任何帮助!
以下步骤解决了问题:
- 文件 - 使 IntelliJ 中的 Chaches 无效,清除文件系统和本地历史记录,清除日志缓存和索引,清除共享索引。 之后我收到关于我的 Node v16 的警告。
- 切换到节点 v14,从项目根目录
rm node_modules
然后 运行 npm i
。
我的 nestjs 应用程序在使用 mongoose 连接到 mongodb 时出现奇怪的故障:
[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 以及通过 'mongodb://127.0.0.1:27017' 或 'mongodb://localhost' 使用 Intellij DB 选项卡完美地达到它,但不能通过嵌套应用程序。
这是我的 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 版本(项目要求)。所以这似乎是我这边的问题,但话又说回来,自从上一个 nest 项目以来我没有改变任何东西。
- 连接失败的原因是什么mongo?
- 如何debug/trace这个错误?
感谢任何帮助!
以下步骤解决了问题:
- 文件 - 使 IntelliJ 中的 Chaches 无效,清除文件系统和本地历史记录,清除日志缓存和索引,清除共享索引。 之后我收到关于我的 Node v16 的警告。
- 切换到节点 v14,从项目根目录
rm node_modules
然后 运行npm i
。