Reputation: 291
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
Upvotes: 2
Views: 2300
Reputation: 81
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.
Upvotes: 0
Reputation: 291
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
Upvotes: 1