InMemoryWebApiModule - status: 404 - collection not found

Guy E

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

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Status: 404-Not found

HTTP Status 404 - Servlet not found

Servlet: HTTP Status 404 - Not Found

Spring type=Not Found, status=404 No message available

Maven web project HTTP Status [404] – [Not Found]

Eclipse & Tomcat Error: HTTP Status 404 – Not Found:

HTTP 404 status code (Not Found) shown as 302

Spring Boot, Status 404, Error "Not Found"

HTTP Status 404 – Not Found Spring Boot

HTTP Status 404 – Not Found tomcat spring

HTTP Status [404] ? [Not Found] for /login in spring security

API request returning status code 404 not found

NOT FOUND (status code: 404): Unknown Message

HTTP Status 404 – Not Found on Tomcat server

JSP/JDBC: HTTP Status 404 - Not Found

HTTP Status 404 - Not Found - Servlet and JSP

"status": 404, "error": "Not Found", "path": "/GetProduct"

HTTP Status 404 – Not Found: for controller - no view resolvers found in Idea

Failed to load resource: the server responded with a status of 404 (Not Found). with ajax in caph

Microsoft Graph API status code 404 not found error

Insertion of image in mysql using jsp HTTP Status 404 not found

Spring Boot Whitelabel Error page (type=Not Found, status=404)

How to redirect "HTTP Status 404 – Not Found" to an error information page in tomcat?

Issue With Spring: There was an unexpected error (type=Not Found, status=404)

Spring Mvc get HTTP Status [404] – [Not Found] after the submission

Spring Mvc App get - HTTP Status [404] – [Not Found]

Ionic 4 - Unhandled Promise rejection: Error Status 404: App not found

Failed to load COMPASS resource: the server responded with a status of 404 (Not Found)

Failed to load resource: the server responded with a status of 404 (Not Found)