infra-geo-ouverte / igo2-lib

Librairie Infrastructure Géomatique Ouverte 2.0 (IGO-2) / Open GIS Infrastructure 2.0 Library
https://infra-geo-ouverte.github.io/igo2-lib/
40 stars 25 forks source link

Getting NullInjectorError: R3InjectorError No provider for SearchSourceService! #786

Closed sukeshlaghate closed 3 years ago

sukeshlaghate commented 3 years ago

For one of projects I am integrating igo2-lib as mapping library, so far this has proven to be a wonderful library. I am able to implement identify Operation ( when user clicks on map, the application queries layers at point clicked by user and return results).

However when I try to display returned results in igo-search-results component I keep getting NullInjectorError, mentioned in the subject/title of this issue. I am including snippets of the code that I have written. in production.ts where I set the searchSources as part of configuration

searchSources: {
      nominatim: {
        enabled: true,
      },
      coordinatesreverse: {
        showInPointerSummary: true,
        enabled: true,
      },
      ilayer: {
        searchUrl: '/api/layers/search',
        order: 4,
        enabled: true,
        params: {
          limit: '50',
        },
      },
    },

In the Dynamically loaded module where map is created I have imported

@NgModule({
  declarations: [...ROUTED_GEOINSIGHTS_COMPONENTS],
  imports: [
    CommonModule,
    ...
    IgoQueryModule.forRoot(),
    IgoSearchModule.forRoot(),
    ...
  ],
  providers: [
    provideCoordinatesReverseSearchSource(),
    provideILayerSearchSource(),
    provideNominatimSearchSource(),
  ],
})
export class GeoinsightsModule {}

Map component code where I am handling Query results

  public handleQueryResults(event: {
    features: Feature[],
    event: OlMapBrowserPointerEvent
  }) {
    const querySearchSourceArray: QuerySearchSource[] = [];

    const results = event.features.map((feature: Feature) => {
      let querySearchSource = querySearchSourceArray.find(
        (source) => source.title === feature.meta.sourceTitle
      );
      if (!querySearchSource) {
        querySearchSource = new QuerySearchSource({
          title: feature.meta.sourceTitle,
        });
        querySearchSourceArray.push(querySearchSource);
      }
      return featureToSearchResult(feature, querySearchSource);
    });
    this.queryStore.load(results);
 }

In the corresponding HTML code for map component I check if the queryStore is loaded and call igo-search-result component like so

<igo-search-results [map]="map" [store]="queryStore" [showIcons]="true"
                            [withZoomButton]="withZoomButtons" (resultFocus)="focusResult($event)"
                            (resultUnfocus)="unfocusResult($event)" (resultMouseenter)="focusResult($event)"
                            (resultMouseleave)="unfocusResult($event)" (resultSelect)="selectResult($event)">
                        </igo-search-results>

I would appreciate if you could show me what I am missing and point me in right direction for achieving the desired functionality.

Following is the actual error I am getting

 ERROR NullInjectorError: R3InjectorError(GeoinsightsModule)[SearchService -> SearchService -> SearchService -> SearchSourceService -> SearchSourceService -> SearchSourceService]: 
  NullInjectorError: No provider for SearchSourceService!
    at NullInjector.get (http://localhost:4200/vendor.js:32885:27)
    at R3Injector.get (http://localhost:4200/vendor.js:46892:33)
    at R3Injector.get (http://localhost:4200/vendor.js:46892:33)
    at R3Injector.get (http://localhost:4200/vendor.js:46892:33)
    at injectInjectorOnly (http://localhost:4200/vendor.js:32740:33)
    at Module.ɵɵinject (http://localhost:4200/vendor.js:32750:57)
    at Object.SearchService_Factory [as factory] (http://localhost:4200/geoinsights-geoinsights-module.js:12678:144)
    at R3Injector.hydrate (http://localhost:4200/vendor.js:47130:63)
    at R3Injector.get (http://localhost:4200/vendor.js:46880:33)
    at NgModuleRef$1.get (http://localhost:4200/vendor.js:64193:33)

Following is the information of my environment Please tell us about your environment:

With regards, Sukesh.

sukeshlaghate commented 3 years ago

After digging around the codebase for search in igo2-lib repo I found that SearchService was not part of any providers. Once I included it in IgoSearchModule and rebuilt the libs my integration started working like charm. @mbarbeau Request you to review and add this line of code in IgoSearchModule

...
import { provideSearchSourceService } from './shared/search-source-service.providers';
import { SearchService } from './shared';
...

export class IgoSearchModule {
  static forRoot(): ModuleWithProviders<IgoSearchModule> {
    return {
      ngModule: IgoSearchModule,
      providers: [
        provideSearchSourceService(),
        provideDefaultIChercheSearchResultFormatter(),
        provideDefaultCoordinatesSearchResultFormatter(),
        provideILayerSearchResultFormatter(),
        SearchService, // <=== this is missing in the IgoSearchModule
      ],
    };
  }
}
mbarbeau commented 3 years ago

Thank you for the finding. I added the proposed line of code. Best regards