Почему я получаю «Гнездо не может разрешить зависимости» - PullRequest
0 голосов
/ 19 октября 2019

Я пытаюсь выяснить, почему я получаю следующую ошибку:

Nest не может разрешить зависимости от UniqueInteractionsService (?). Убедитесь, что зависимость аргумента в индексе [0] доступна в контексте SharedModule.

мои классы:

sharedModule.ts:

import {Module, Global} from '@nestjs/common';
import {InteractionsService} from "./elasticsearch/interactionsService";
import {UniqueInteractionsService} from "./elasticsearch/uniqueInteractionsService";
import {EsProvider} from "./elasticsearch/esProvider";

@Global()
@Module({
    exports: [InteractionsService, UniqueInteractionsService],
    providers: [EsProvider, InteractionsService, UniqueInteractionsService]
})
export class SharedModule {
}

interacionsService.ts:

import {ESService} from "./ESService";
import {Injectable, Inject} from '@nestjs/common';

@Injectable()
export class InteractionsService{

    constructor(@Inject(ESService) private readonly esService: ESService) {}

    // more stuff
}

uniqueInteractionsService.ts:

import {ESService} from "./ESService";
import {Injectable, Inject} from '@nestjs/common';

@Injectable()
export class UniqueInteractionsService{
    constructor(@Inject(ESService) private readonly esService: ESService) {}

    // more stuff
}

esProvider.ts:

import {ESService} from "./esService";

export const EsProvider = {
    provide: ESService,
    useFactory: async () => {
        const esService = new ESService();
        await esService.init();
        return esService;
    }
};

1 Ответ

0 голосов
/ 22 октября 2019

Вы экспортируете UniqueInteractionsService из SharedModule. В целевом модуле вам необходимо импортировать этот SharedModule, и тогда вы сможете использовать UniqueInteractionsService в targetService, принадлежащем TargetModule, через внедрение зависимостей.

Пример:

target-module.ts:
@Module({
  imports: [SharedModule]
})
export class TargetModule {}
...