InMemoryWebApiModule - 状态:404 - 未找到集合

时间:2018-01-01 11:20:14

标签: angular typescript angular2-services in-memory

我正在尝试使用angular2 InMemoryWebApiModule并实现InMemoryDbService,以创建我的后端服务器的模型。我将它配置为(我认为)它应该是,但不知何故,我无法到达实现InMemoryDbService(worker.ts中的WorkerData)的数据结构并继续获取状态404(“未找到”集合'工作者')。我简化了代码以使其更清晰。这是一个URL问题 - 无法弄清楚原因。

在服务中执行getWorkrs函数时会出现问题。 看我的代码: 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(与上面的worker.ts位于同一文件夹中):

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 个答案:

答案 0 :(得分:1)

您似乎错误配置了从createDb返回的对象。此docs状态表示此对象为:

  

哈希,其键是集合名称,其值是要返回或更新的集合对象的数组。

在您的示例中,您将从createDb返回一个数组,这是您自己的workers集合。相反,您需要按照描述返回一个对象。即。

return { workers: workers };

-OR -

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