InMemoryWebApiModule - status: 404 - collection not found

£可爱£侵袭症+ 提交于 2019-12-11 13:42:45

问题


I'm trying to use angular2 InMemoryWebApiModule and implement the InMemoryDbService, to create a mock-up of my backend server. I configured it as (I thought) it should be, but somehow I can't reach the data structure that implement the InMemoryDbService (WorkerData in worker.ts) and keep getting status 404 ("Collection 'workers' not found"). I've simplified the code to make it clearer. This is a URL issue - can't figuring out why.

The problem occurs when executing the getWorkrs function in the service. See my code: App.module.ts:

import { WorkerData } from './Services/worker';
import {InMemoryWebApiModule } from 'angular-in-memory-web-api';
@NgModule({
 imports: [ BrowserModule, HttpModule, ReactiveFormsModule, FormsModule, 
    RouterModule, routes, InMemoryWebApiModule.forRoot(WorkerData) ],
 declarations: [ ... ],
providers: [ WorkerService],
bootstrap: [ AppComponent ]
})
export class AppModule { }

worker.ts:

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

export interface IWorker
{
    id: number;
    IDNumber: number;
    FirstName?:string;
    LastName?:string;
    Email? : string;
    DateOfBirth?: string;
    PhoneNumber1?: string;
    PhoneNumber2?: string;
    Street1? :string;
    Street2?: string;
    City?: string;
    Zip?:number;
    General_Additional_Data?: string;
}

 @Injectable()
 export class WorkerData implements InMemoryDbService
 {
     createDb()
     {
       let workers = 
        [{
         "id": 1,
         "IDNumber": 289156,
         "FirstName":"qqqqqq",
         "LastName":"cccccc",
         "DateOfBirth": "02/11/1991",
         "PhoneNumber1": "3422-088808",
         "PhoneNumber2":"3234-6321223",
         "General_Additional_Data": "qqqqqqqqqqqqq"
     },
     {
         "id": 2,
         "IDNumber": 289157,
         "FirstName":"aaaaaaa",
         "LastName":"bbbbbbb",
         "DateOfBirth": "05/11/1981",
         "PhoneNumber1": "02328-08434348",
         "PhoneNumber2":"052322-63213434",
         "General_Additional_Data": "efefsdfsdfs"
     },
     {
         "id": 3,
         "IDNumber": 289466,
         "FirstName":"aaaaa",
         "LastName":"bbbbbb",
         "DateOfBirth": "05/1/1991",
         "PhoneNumber1": "0328-45634348",
         "PhoneNumber2":"3232-7613434",
         "General_Additional_Data": "aaaaaaaaaaaaaaaaa"
     }];
     return workers;
 }
 }


export class Worker implements IWorker{

constructor(
    public id =-1,
    public IDNumber = -1,
    public FirstName?:string,
    public LastName?:string,
    public Email? : string,
    public DateOfBirth?: string,
    public PhoneNumber1?: string,
    public PhoneNumber2?: string,
    public Street1? :string,
    public Street2?: string,
    public City?: string,
    public Zip?:number,
    public General_Additional_Data?: string
){}

}

WorkerService.service (in the same folder as the worker.ts above is):

import { Injectable } from '@angular/core';
import { Http, Response} from '@angular/http';
import { Observable } from 'rxjs/Observable';

import { IWorker, WorkerData } from './Services/worker';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/of';

@Injectable()

export class WorkerService
{
    private _workerURL = 'api/workers';
    constructor (private _http: Http){}
        getWorkers(): Observable<IWorker[]>
        {
            return this._http.get(this._workerURL)
                       .map((response : Response) => <IWorker[]> 
                        response.json())
                        .do(data => console.log('All: ' + 
                        JSON.stringify(data)))
                       .catch(this.handleError);
       }

回答1:


It looks like you've misconfigured the object being returned from the createDb. The docs state that this object is a:

hash whose keys are collection names and whose values are arrays of collection objects to return or update.

In your example, you are returning an array from createDb, which is your workers collection itself. Instead, you need to return an object as described. i.e.

return { workers: workers };

-or-

return { workers }; // Shorthand for the above.


来源:https://stackoverflow.com/questions/48049219/inmemorywebapimodule-status-404-collection-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!