Im trying to fetch one object (Type 'Coupon') from DB , and show it when I click on object from list in HTML.
The problem is, When the page loads on, the object details appear the way I wanted it to,But! the consםle also shows "ERROR TypeError: Cannot read property 'imageURL' of undefined".
My code:
step - 1: storage.service.ts (HTTP Request):
@Injectable()
export class StorageService {
token: string
constructor(private http: HttpClient) { }
fetchCouponById(couponId: string): Observable<Coupon> {
const params = new HttpParams()
.set('token', this.token)
.set('couponId', couponId)
return this.http.get<Coupon>('http://localhost:8080/api/company/get_coupon', { params })
}
step - 2: company.service.ts :
@Injectable()
export class CompanyService {
@Output() selectedCoupon = new EventEmitter<Coupon>()
coupon: Coupon
fetchCouponById(id: string) {
this.storagService.fetchCouponById(id)
.subscribe(coupon => {
this.coupon = coupon
this.onSelectCoupon()
})
}
onSelectCoupon() {
this.selectedCoupon.emit(this.getCoupon())
}
getCoupon() {
return this.coupon
}
step - 3: company-coupons-page.ts:
export class CompanyCouponsPageComponent implements OnInit {
coupon: Coupon
selectedCoupon: string
constructor(
private companyService: CompanyService,
private route: ActivatedRoute) { }
ngOnInit(): void {
this.route.params.subscribe((params: Params) => { this.selectedCoupon = params['id'] })
this.companyService.fetchCouponById(this.selectedCoupon)
this.companyService.selectedCoupon.subscribe(
(coupon: Coupon) => { this.coupon = coupon})
}
company-coupons-page.HTML:
<div class="container">
<div class="row">
<div class="col-6"
style="height: 70%;
width: 40%;">
<img src={{coupon.imageURL}}
alt="..."
style="width: 100%;height: 100%; ">
</div>
<div class="col-6">
<h1>{{coupon.title}}</h1>
<h3>{{coupon.amount}}</h3>
<h3>{{coupon.category}}</h3>
</div>
</div>