0

I have auto-generated a WebAPI service module with NSwagStudio to connect my Ionic/Angular app and my webservice together. This service (WebApiConnector.Client) is configured to be injected:

imports: [
    BrowserModule,
    IonicModule.forRoot(),
    AppRoutingModule,
    AngularFireModule.initializeApp(firebaseConfig),
    AngularFireAuthModule,
    HttpClientModule
  ],
  providers: [    
    GooglePlus,
    StatusBar,
    SplashScreen,
    { provide: WebApiConnector.API_BASE_URL, useValue: environment.apiURL },
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
    { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptorService, multi: true },
    { provide: ErrorHandler, useClass: GlobalErrorHandler},
    AuthService,
    WebApiConnector.Client

  ],

When I run the app on browser or debug it on android device, all works like a charm, but when I build it in production with:

ionic cordova build android --prod --release

the HttpClient injected into WebAPI service seems to be undefined because when the first call is performed, the app falls into this exception:

Uncaught (in promise): TypeError: Cannot read property 'request' of undefined

The call is performed inside ngAfterViewInit() hook.

I also tried to inject HttpClient into the page where "WebApiConnector.Client" is injected to perform manually the request to the server, but it perfectly worked.

This is the service module configuration

import { HttpClient, HttpHeaders, HttpResponse, HttpResponseBase } from '@angular/common/http';

@Injectable({
    providedIn: 'root'
})
export class Client {
    private http: HttpClient;
    private baseUrl: string;
    protected jsonParseReviver: ((key: string, value: any) => any) | undefined = undefined;

    constructor(@Inject(HttpClient) http: HttpClient, @Optional() @Inject(API_BASE_URL) baseUrl?: string) {
        this.http = http;
        this.baseUrl = baseUrl ? baseUrl : "/webapi_2";
    }

and this is the snippet of code that throw the exception:

return this.http.request("get", url_, options_)

Where is the problem?

MFeltrin
  • 3
  • 5
  • Are you sure that the error is for that particular line? – David Apr 26 '20 at 13:07
  • I'm pretty sure, because I've put a chek before that line, if (typeof this.http == 'undefined') { alert('http is undefined'); } and when I run it, the alert is displayed before the exception. – MFeltrin Apr 26 '20 at 14:20
  • Are the providers above the ones from AppModule? Can you try removing the Client from the providers array? And in which module is the page using the client? – David Apr 26 '20 at 18:56
  • What do you mean with "Are the providers above the ones from AppModule?"? If I remove Client from providers array I receive "NullInjectorError". I think that the page using the client has no module, or I guess so, I'm not a master of Angular, sorry. – MFeltrin Apr 26 '20 at 20:32
  • I tried to remove the module WebApiConnector where the Client class is encapsulated and wIthout it, the injection of httpclient works. I tried to search some info about this problem without results. Do you have some advices? – MFeltrin Apr 28 '20 at 18:33
  • Sorry, I don't know – David Apr 29 '20 at 08:04

0 Answers0