Search code examples
angularangular2-routingangular2-templateangular2-services

How to In memory web api for two different jsons


I am using In memory web api for two json services(DB)

1)

import {InMemoryDbService} from 'angular-in-memory-web-api';
export class InMemoryDataService implements InMemoryDbService {
    createDb() {
        let heroes = [
            {id: 11, name: 'Mr. Nice'},
            {id: 12, name: 'Narco'},
            {id: 13, name: 'Bombasto'},
            {id: 14, name: 'Celeritas'},
            {id: 15, name: 'Magneta'},
            {id: 16, name: 'RubberMan'},
            {id: 17, name: 'Dynama'},
            {id: 18, name: 'Dr IQ'},
            {id: 19, name: 'Magma'},
            {id: 20, name: 'Tornado'}
        ];
        return {heroes};
    }
}      

2)

import {InMemoryDbService} from 'angular-in-memory-web-api';

import {Address} from "cluster";

export class StudentData implements InMemoryDbService {
    createDb() {

        let students = [
            {
                id: 11,
                FirstName: 'Mounika',
                LastName: 'Gangamwar',
                email: 'asa@gmailc.com',
                Phonenumber: 1111,
                Address: '2323',
                Password: 'asa',
                CPassword: 'aa'
            }
        ];
        return {students};
    }
}

My app.module.js is

import {InMemoryWebApiModule} from 'angular-in-memory-web-api';

import {InMemoryDataService}  from './in-memory-data.service';

import {StudentData} from './forms/student.data.service';

@
NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        InMemoryWebApiModule.forRoot(InMemoryDataService, StudentData)
    ],
    declarations: [AppComponent],
    bootstrap: [UIView]
})

My problem is I'm not able to add two db in InMemoryWebApiModule.forRoot(InMemoryDataService,StudentData)]

I am getting 404 error for StudentData service. If I remove one dbService from it's working fine

My doubt is how to add two dbservices to forRoot method?


Solution

  • You can use one service for both and return both with the name of the collection the solution.

    Here is InMemoryDataService

    import {InMemoryDbService} from 'angular-in-memory-web-api';
    export class InMemoryDataService implements InMemoryDbService {
        createDb() {
            let heroes = [
                {id: 11, name: 'Mr. Nice'},
                {id: 12, name: 'Narco'},
                {id: 13, name: 'Bombasto'},
                {id: 14, name: 'Celeritas'},
                {id: 15, name: 'Magneta'},
                {id: 16, name: 'RubberMan'},
                {id: 17, name: 'Dynama'},
                {id: 18, name: 'Dr IQ'},
                {id: 19, name: 'Magma'},
                {id: 20, name: 'Tornado'}
            ];
            let students = [
                {
                    id: 11,
                    FirstName: 'Mounika',
                    LastName: 'Gangamwar',
                    email: 'asa@gmailc.com',
                    Phonenumber: 1111,
                    Address: '2323',
                    Password: 'asa',
                    CPassword: 'aa'
                }
            ];
            return {heroes, students};
        }
    }
    

    and the app module is like that

    import {InMemoryWebApiModule} from 'angular-in-memory-web-api';
    import {InMemoryDataService}  from './in-memory-data.service';
    @NgModule({
        imports: [
            BrowserModule,
            FormsModule,
            InMemoryWebApiModule.forRoot(InMemoryDataService, {apiBase: '/api'})
        ],
        declarations: [AppComponent],
        bootstrap: [UIView]
    })
    

    And the collection name will be generated based on the URL for each students and the heroes "/api/heroes" && "/api/students".