Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't resolve dependencies DbConnectionToken (?) #1525

Closed
kalicki opened this issue Feb 6, 2019 · 2 comments
Closed

Can't resolve dependencies DbConnectionToken (?) #1525

kalicki opened this issue Feb 6, 2019 · 2 comments

Comments

@kalicki
Copy link

kalicki commented Feb 6, 2019

I'm submitting a...


[ ] Regression 
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Nest can't resolve dependencies of the DbConnectionToken (?). Please make sure that the argument at index [0] is available in the DatabaseModule context.

Hey! I have the error above, but I could not identify the causer, followed the examples:
https://github.com/nestjs/nest/tree/master/sample/14-mongoose-base/src/database

But without success, the only thing that's different is the ConfigService calls

Minimal reproduction of the problem with instructions

database.module.ts

import { Module } from '@nestjs/common';
import { databaseProviders } from './database.providers';

@Module({
  components: [...databaseProviders],
  exports: [...databaseProviders],
})
export class DatabaseModule {}

database.providers.ts

import * as mongoose from 'mongoose';
import { ConfigService } from '../config/config.service';

export const databaseProviders = [
  {
    provide: 'DbConnectionToken',
    useFactory: async (
      configService: ConfigService,
    ): Promise<typeof mongoose> => {
      const connection = await mongoose.connect(
        `mongodb+srv://${configService.get('DATABASE_HOST')}`,
        {
          user: configService.get('DATABASE_USER'),
          pass: configService.get('DATABASE_PASS'),
          dbName: configService.get('DATABASE_NAME'),
          useNewUrlParser: true,
          useCreateIndex: true,
        },
      );
      return connection;
    },
    inject: [ConfigService],
  },
];

recipe.module.ts

import { Module } from '@nestjs/common';
import { RecipesController } from './recipes.controller';
import { RecipesService } from './recipes.service';
import { recipesProviders } from './recipes.providers';
import { DatabaseModule } from '../database/database.module';

@Module({
  imports: [DatabaseModule],
  controllers: [RecipesController],
  providers: [RecipesService, ...recipesProviders],
})
export class RecipesModule {}

app.module.ts

import { Module } from '@nestjs/common';
import { ConfigModule } from './config/config.module';
import { RecipesModule } from './recipes/recipes.module';

@Module({
  imports: [ConfigModule, RecipesModule],
})
export class AppModule {}

Environment


Nest version: 5.7.0

 
For Tooling issues:
- Node version: XX  10.15.0
- Platform:  Mac

Others:

@kalicki kalicki changed the title Nest can't resolve dependencies of the DbConnectionToken (?) Can't resolve dependencies DbConnectionToken (?) Feb 6, 2019
@kamilmysliwiec
Copy link
Member

kamilmysliwiec commented Feb 6, 2019

You have to import ConfigModule in your DatabaseModule.

@lock
Copy link

lock bot commented Sep 24, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants