I am facing an issue with a private variable named product that I declare in the constructor. Inside the ngOnInit function, I am using array.forEach() to loop through some sections from a service and assign a value to the product variable. Even though console.log displays the correct value, I keep getting an error stating 'Cannot Read Properties of undefined' related to product. Can someone please help me figure out what mistake I might be making?
private product: string;
constructor(private service: Service) {
this.product = "";
}
ngOnInit() {
this.service.sections.forEach(function (value) {
if (this.product) {
this.product = value.Product.ShortName;
} else {
this.product = "";
}
console.log(value.Product.ShortName);
});
}