0

I try to use the angular-in-memory-web-api (version 0.5.1). This works fine if I set up the "database" with local objects, but it fails if I try to get my data from local JSON files over an http request with following error:

Uncaught Error: Provider parse errors: Cannot instantiate cyclic dependency! HttpClient ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1

as soon as I import the httpClient package into my service.

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
// Imports for loading & configuring the in-memory web api
import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService } from 'app/shared/services/in-memory-data.service';

import { AppComponent } from './app.component';
// other imports of app components

@NgModule({
  imports:      [
    BrowserModule,
    HttpClientModule,
    HttpClientInMemoryWebApiModule.forRoot(InMemoryDataService),    // always import after the HttpClientModule
  ],
  declarations: [
    AppComponent,
    // ...
  ],
  providers: [ // app wide services not concerning the problem ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

in-memory-data.service.ts:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { InMemoryDbService } from 'angular-in-memory-web-api';
import { RequestInfo } from 'angular-in-memory-web-api';

@Injectable()
export class InMemoryDataService implements InMemoryDbService {       
  // constructor(http: HttpClient) { // this creates the cyclic dependency
  constructor() {

  }    
  createDb(reqInfo?: RequestInfo) {
    const db = {}   
    // fetch data from local JSON files and set up "database" object    
    return db;
  }

}

Is this problem related to the HttpClientModule or is it an angular-in-memory-web-api problem?

4
  • Well, don't use InMemoryWebApiModule if you are looking to make requests to json file. For that, just make http get requests with the httpclient. Commented Nov 6, 2017 at 19:15
  • Initializing the in-memory database service from a JSON files is what I want to do and therefore I have to request JSON files because it's quite some data... Commented Nov 6, 2017 at 19:37
  • yes, request the json files, NOT inmemorywebapi. Commented Nov 6, 2017 at 19:48
  • Have you found the answer for the same? Commented Dec 12, 2017 at 11:02

1 Answer 1

1

I have faced same problem and that is how I was able to solve it, do not inject in the constructor but try to do that in the method.

From your createDB you will have httpclient injected correctly without the cyclic error.

import { Injectable, Injector } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { InMemoryDbService } from 'angular-in-memory-web-api';
import { RequestInfo } from 'angular-in-memory-web-api';

@Injectable()
export class InMemoryDataService implements InMemoryDbService {  
  httpClient: HttpClient;
  constructor(private inject: Injector) {

  }    
  createDb(reqInfo?: RequestInfo) {
    const db = {}   
    this.httpClient = this.inject.get(HttpClient);
    // fetch data from local JSON files and set up "database" object    
    return db;
  }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.