My Component code:
products: Product[];
constructor(private productService: ProductService) { }
ngOnInit() {
this.productService.getProducts()
.subscribe(
(products: Product[]) => {
this.products = products;
}
);
}
My HTML Code:
<tr *ngFor="let product of products">
<td>
<a [routerLink]="['/product-details', product.SKU]">{{ product.SKU }} </a>
</td>
<td>
<a [routerLink]="['/product-details', product.SKU]">{{ product.mrp }} </a>
</td>
</tr>
My Service Code:
getProducts() {
return this.http.get('http://localhost:3000/product')
.map((response: Response) => {
const products = response.json().obj;
let transformedMessages: Product[] = [];
for (let product of products) {
transformedMessages.push(new Product(product.SKU, product.mrp, 111, 111));
}
this.products = transformedMessages;
return transformedMessages;
return products;
})
.catch((error: Response) => Observable.throw(error.json()));
My Backend route:
router.get('/', function (req, res, next) {
Product.find().exec(function (err, products) {
if (err) {
return res.status(500).json({
title: 'An error occurred',
error: err
});
}
res.status(200).json({
message: 'Success',
obj: products //obj comes from here
});
});
});
Response:
{
"message": "Success",
"obj": [
{
"0": {
"SKU": "V2620151BR",
"Batch": "1",
"Folder Number": "85",
"MRP": "799",
"Size": "Free",
"Color": "Yellow",
},
My Backend Model Class:
var schema = new Schema({
sku: { type: String, required: true, unique: true },
mrp: { type: Number }
});
My Frontend Model:
export class Product {
constructor(
public SKU: string,
public mrp: number,
public selling_price: number,
public current_inventory: number
) { }
}
My http://localhost:3000/product
reutrns the json response correctly. But somehow in the HTML, when the page loads everything is empty.
I am only getting one value - empty empty 111 111
Most of the things are setup fine, I am not sure why the empty HTML when I get the response back from http://localhost:3000/product