I'm currently working on updating my JSON response by adding a new object property.
Below is an example of my initial JSON response:
{
"products": [{
"id": 1,
"name": "xyz"
}]
}
My goal is to include a new object property called 'show' in the same JSON response:
{
"products": [{
"id": 1,
"name": "xyz",
"show": false
}]
}
Here is a snippet of my code implementation:
import {Component,OnInit} from '@angular/core';
import {Http, Response} from '@angular/http';
import 'rxjs/Rx';
@Component({
templateUrl:'./electronics.html'
})
export class electronicsComponent{
productListElectronics={};
productListElectronicResponse: Object;
error:Object;
constructor(private http: Http) {
this.productListElectronicResponse={};
this.error={};
http.get('../json/electronics/electronics.json').map((res: Response) => res.json()).subscribe(res =>
{
for(var i = 0; i <= res['products'].length; i++){
res['products'][i].show=false;
}
console.log(res);
}, error => this.error = error );
}
}
However, I keep encountering the following error message in the console:
ERROR TypeError: Cannot set property 'show' of undefined at SafeSubscriber.http.get.map.subscribe._this.error [as _next] (products.component.ts:22) at SafeSubscriber.__tryOrUnsub (Subscriber.ts:238) at SafeSubscriber.next (Subscriber.ts:190) at Subscriber._next (Subscriber.ts:135) at Subscriber.next (Subscriber.ts:95) at MapSubscriber._next (map.ts:80) at MapSubscriber.Subscriber.next (Subscriber.ts:95) at XMLHttpRequest.onLoad (xhr_backend.ts:104) at ZoneDelegate.invokeTask (zone.js:398) at Object.onInvokeTask (ng_zone.ts:253) at ZoneDelegate.invokeTask (zone.js:397) at Zone.runTask (zone.js:165) at XMLHttpRequest.ZoneTask.invoke (zone.js:460)