2

I am trying to add a new custom DataService to @ngrx/data

I am extending DefaultDataService class through custom-entity-data-service.ts and then registering it to entityService to get the service as DI

custom-entity-data-service.ts

Step 1 - Define the service

import { Injectable } from '@angular/core';
import { DefaultDataService, HttpUrlGenerator, DefaultDataServiceConfig } from '@ngrx/data';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { SharepointService } from 'dnp-sharepoint';

// @Injectable() << NOTE - this commented out
export class SharePointEntityDataService extends DefaultDataService<any>{

  constructor( entityName: string,  http: HttpClient, 
     httpUrlGenerator: HttpUrlGenerator, config: DefaultDataServiceConfig, private sharePoint: SharepointService) {
    super(entityName, http, httpUrlGenerator, config);
}
  
getAll(): Observable<any[]>{
  console.log('calling for ',this.entityName);
  return this.sharePoint.getAll(this.entityName); // << my custom call 
}

}

app-store.module.ts << module encapsulating all ngrx/data stuff

Step 2 - In the module class register the service for the entity

@NgModule({
  imports: [
    CommonModule,
    HttpClientModule,
    StoreModule.forRoot({}),
    EffectsModule.forRoot([]),
    EntityDataModule.forRoot(entityConfig),
    environment.production ? [] : StoreDevtoolsModule.instrument(),
  ],
  providers: [Store, SharePointEntityDataService]
})
export class AppStoreModule {
  constructor(private entityDataService: EntityDataService, private sharepointEntityDataService: SharePointEntityDataService) {
    this.entityDataService.registerService('Registry', this.sharepointEntityDataService);
  }
}

simple.component.ts

Step 3 - Use it in component

  constructor(private registryService: RegistryService, private entityDataService: EntityDataService, 
    private sharePointDataService: SharePointEntityDataService) { 
      this.registryService.getAll().subscribe(obj=>console.log('ger all ...',obj));
    }
ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[SharePointEntityDataService -> String]: 
  StaticInjectorError(Platform: core)[SharePointEntityDataService -> String]: 
    NullInjectorError: No provider for String!
NullInjectorError: StaticInjectorError(AppModule)[SharePointEntityDataService -> String]: 
  StaticInjectorError(Platform: core)[SharePointEntityDataService -> String]: 

I think the error is due to some dependency injection, but not able to figure out. Any suggestions will be greatly appreciated.

Thanks

Community
  • 1
  • 1
maxkart
  • 619
  • 5
  • 21

0 Answers0