I am encountering an issue while trying to set the reviews for an Angular 2 project. When I try to assign a value to this.reviews = targ, I receive an error message: TypeError: Cannot set property 'reviews' of undefined. However, I know that targ exists because I can successfully print it to the console. Any thoughts on why this might be happening?
import { ReviewService } from '../review.service';
import { Review } from '../review/review.component'
import { Component} from '@angular/core';
import { OnInit } from '@angular/core';
@Component({
selector: 'review-list',
templateUrl: './review-list.component.html',
styleUrls: ['./review-list.component.css'],
providers: [ReviewService] //for the injector to be able to inject ReviewerService
})
export class ReviewListComponent implements OnInit {
public reviews: Review[];
constructor(private reviewService: ReviewService) {
this.reviews = [] ;
}
initializeReviews(): void {
this.reviewService.getReviews().then(
this.set
).catch(function(reason){
console.log(reason);
});
}
set(targ):void {
console.log(targ);
this.reviews = targ;
}
ngOnInit(): void {
this.initializeReviews();
//this.reviews = this.reviewService.get();
}
}