k1k4ss0
k1k4ss0

Reputation: 87

Mongo doesn't let NestJS create a new DB

I have a backend with NestJS, it is configured with mongoose. The app.module.ts :

@Module({
  imports: [
    MongooseModule.forRoot('mongodb://admin:root@localhost:27017/'),
    CatsModule
  ],
  controllers: [],
  providers: [],
})

If I let the URI like this it works correctly but creates a 'tests' database. I want a custom database, so when I try to set the URI to mongodb://admin:root@localhost:27017/my-custom-db it cant connect...

I've tried to create my-custom-db manually, but it still fails

The error :

[Nest] 14480  - 26/06/2022, 12:28:27   ERROR [MongooseModule] Unable to connect to the database. Retrying (1)...

Upvotes: 1

Views: 778

Answers (1)

k1k4ss0
k1k4ss0

Reputation: 87

The Problem was because Mongoose use a diferent format to connect to a specific database, we have to pass the name by option:

@Module({
  imports: [
    MongooseModule.forRoot(
    'mongodb://localhost:27017',
    {useNewUrlParser:true,user:"root",pass:"example",dbName:"nest"}),
    UserModule],
  controllers: [AppController],
  providers: [AppService],
})

Upvotes: 1

Related Questions