2

How to use Http methods of Angular2 with in-memory-web-api?

My company has currently started an application and we are using Angular2 as a front end choice. Since we still do not have back end part we are using in-memory-web-api . I have a json file located in assets folder of Angular2 structure and I have built CRUD using Http methods and Observables. If I comment out **in-memory-web-api** I am able to make a get request to all my objects of json file but I am not able to do a post, or get an object by parameter so I need "in-memory-web-api" . If I use "in-memory-web-api" when I try to get all the objects of the json file I got the followin error

Collection 'product' not found

3
  • Did you get it resolved? I've also got into same problem! Commented Apr 6, 2017 at 9:16
  • @DhavalMarthak Yes I got it. The problem in your case should be with the url. Let say if you have two different json objects put them in a single file and return at the end both of them. For instances if you have in your application api/infos and api/products then both infos and products put in the same file and return them both in that file. Then you can use the URL as above Commented Apr 6, 2017 at 13:56
  • @DhavalMarthak Look at my answer below Commented Apr 6, 2017 at 14:02

2 Answers 2

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

import { IProduct } from './product';
import {IInfo} from '../Informations/Model/info'

export class ProductData implements InMemoryDbService{

createDb() {

 let infos : IInfo[] = [

 {   "id":1,
    "Name": "Some Name",
   "Surname": "Bla Bla bla",
   "Country": "Austria",     
  "otherInfo":['24','Wien']

},
   {
   "id":2,
  "Name": "Another Name",
    "Surname": "Nla bla",
    "Country": "Norway",
    "otherInfo":['24','Trondheim']

  }


];


 let products: IProduct[] = [
       {              
               'id':1,
             'productName': 'IPAD',
             'productCode': 'GDN-0011',
             'releaseDate': 'March 19, 2016',
             'description': 'Easy Use',
             'price': 1945.95,
             'starRating': 4.2,
            'imageUrl': '',

         },
         {
             'id': 2,
             'productName': 'Mac Book',
             'productCode': 'GDN-0023',
             'releaseDate': 'March 18, 2016',
            'description': 'Excellent',
           'price': 1632.99,
            'starRating': 4.8,
            'imageUrl': ''
        }
    ];
     return { products,infos};
  }
}

Now you can use the url in your services like api/infos or api/products

Sign up to request clarification or add additional context in comments.

Comments

0

One thing i want to highlight in this problem, In case if you are using 'in-memory web API' and if you try to call live running API or you try to call any JSOn file data located in Asset folder you will get the error in console log that particular collection not found. The reason is that 'In -memory-web-Api' will mock your json call or web API call and angular will look in 'in-memory' class which is createDB and eventually it will not get that collection. I would advise you to rather than using 'in memory Web API' , please use collection of JSON file and comment all the In memory Web API reference. It will work fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.