InMemoryWebApiModule-状态:404-找不到集合 [英] InMemoryWebApiModule - status: 404 - collection not found

查看:61
本文介绍了InMemoryWebApiModule-状态:404-找不到集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用angular2 InMemoryWebApiModule并实现InMemoryDbService,以创建后端服务器的模型.我将其配置为(我认为)应该如此,但是以某种方式我无法到达实现InMemoryDbService的数据结构(worker.ts中的WorkerData)并保持状态404(未找到集合'workers'").我简化了代码以使其更加清晰.这是一个URL问题-无法找出原因.

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.

在服务中执行getWorkrs函数时会发生问题. 看我的代码: App.module.ts:

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:

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位于同一文件夹中):

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);
       }

推荐答案

您似乎错误地配置了从createDb返回的对象. 文档指出该对象是:

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.

在您的示例中,您要从createDb返回数组,这是您的workers集合本身.相反,您需要按所述返回一个对象.即

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 };

-或-

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

这篇关于InMemoryWebApiModule-状态:404-找不到集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆