2

I'm trying to get some mocked results for my development environment. I've tried to incorporate angular-in-memory-web-api without much success. Here's my code:

app.module.ts:

@NgModule({
  declarations: [
    AppComponent,
  ],
  imports: [
    ...
    HttpModule,
    ...
    InMemoryWebApiModule.forRoot(MockEventData, {
      passThruUnknownUrl: true
    })
  ],
  providers: [
    ...
    {
      provide: Http,
      useClass: ExtendedHttpService
    },
    ...
    {
      provide: EventService,
      useFactory: (http: Http, userService: UserService, newEventService: NewEventService, router: Router) => {
        if (environment.production) {
          return new EventService(http, userService, newEventService, router)
        } else {
          return new MockEventService(http, userService, newEventService, router)
        }
      },
      deps: [Http, UserService, NewEventService, Router]
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {

mock-event.service.ts:

@Injectable()
export class MockEventService {

  private imageUploadBatch: Observable<Boolean>[];
  private fakeResponse;

  constructor(
    private http: Http,
    private userService: UserService,
    private newEventService: NewEventService,
    private router: Router,
  ) {

  };

  getEvents(excludedEvents: string[]): Observable<Event[]> {
    return this.http
      .post('api/events', excludedEvents)
      .map((res: Response) => res.json())
      .publishLast().refCount()
      .catch((error: any) => Observable.throw(error.json().error || 'Show error.'));
  }
}

mock-event-data.ts:

import { InMemoryDbService } from 'angular-in-memory-web-api';
export class MockEventData implements InMemoryDbService {
  createDb() {
    let events = [
      { id: 1, name: 'Windstorm' },
      { id: 2, name: 'Bombasto' },
      { id: 3, name: 'Magneta' },
      { id: 4, name: 'Tornado' }
    ];
    return { events };
  }
}

The code is quite simple. I made it following this guide: https://angular.io/docs/ts/latest/guide/server-communication.html. However, for whatever reason, the POST for /events always returns {data: Array[0]}.

Any help provided will be deeply appreciated.

Thanks!

2 Answers 2

2

post method will NOT just retrieve the data to angular-in-memory-web-api. Instead it will create the entity accordingly. It's essential and default behavior is to send data to the server same as put and delete. Of course there will be a response to the post request which is probably the data in angular-in-memory-web-api case but remember, this totally depends on the server to response. On the otherget should be used for the purpose of retrieving data from angular-in-memory-web-api as you want.

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

Comments

1

Well, as it turns out, POST seems to not return data in angular-in-memory-web-api. I succeeded in retrieving data by using a GET request. This isn't ideal, but it'll have to work for now.

If someone has a better answer, please provide it, as it's a bit iffy to commit a mock that doesn't use the original request types.

Thanks!

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.