简体   繁体   中英

NestJS + TypeORM - Error connecting to both mysql and mongodb

I am just learning about NestJS and I have been trying to get an application to connect to both mysql and mongodb for some time and I just can't get it to work. The error I am getting is as follows:

Nest can't resolve dependencies of the AppService (UserMRepository, ?). Please make sure that the argument at index [1] is available in the AppModule context.

This is a simple NestJS generated app that simply connects to two databases and will create a record in each database when getHello() is called.

app.module.ts

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './entity/User.entity';
import { UserM } from './entity/User.mongo';


@Module({
  imports: [
    TypeOrmModule.forRoot({
      "name": "default",
      "type": "mongodb",
      "host": "localhost",
      "port": 27017,
      "database": "typeorm",
      "useNewUrlParser": true,
      "useUnifiedTopology": true,
      "entities": [
        "src/**/**.mongo.ts",
        "dist/**/**.mongo.js"
      ],
    }),   
    TypeOrmModule.forRoot({
      "name": "mysql",
      "type": "mysql",
      "host": "localhost",
      "port": 3306,
      "username": "typeorm",
      "password": "typeorm",
      "database": "typeorm",
      "entities": [
        "src/**/**.entity.ts",
        "dist/**/**.entity.js"
      ],
    }),
    TypeOrmModule.forFeature([UserM]),
    TypeOrmModule.forFeature([User], 'mysql'),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

app.service.ts

import { InjectRepository } from '@nestjs/typeorm';
import { Repository, MongoRepository, getRepository, DeleteResult } from 'typeorm';
import { User} from "./entity/User.entity";
import { UserM } from './entity/User.mongo';

@Injectable()
export class AppService {
  constructor(
    @InjectRepository(UserM)
    @InjectRepository(User)
    private readonly userMRepository: Repository<UserM>,
    private readonly userRepository: Repository<User>,
  ) {}

  async getHello(): Promise<string> {
    var userM = new UserM();
    userM.firstName = "Timber";
    userM.lastName = "Saw";
    userM.age = 25;
    var newUser1 = await this.userMRepository.save(userM);

    var user = new User();
    user.firstName = "Timber";
    user.lastName = "Saw";
    user.age = 25;
    var newUser2 = await this.userRepository.save(user);

    return;
  }
}

I can confirm that will successfully work with each database individually but when I try both together it keeps failing. I have read all the posts on this and tried various ways of setting this up so not sure what I am doing wrong. Is someone able to see what I am doing wrong?

Paul

The constructor should be fixed:

constructor(
    @InjectRepository(UserM)
    private readonly userMRepository: Repository<UserM>,
    @InjectRepository(User, 'mysql')
    private readonly userRepository: Repository<User>,
  ) {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM