Currently, I am facing an issue with retrieving a Coupon object from the server using a REST API in combination with Angular. The problem arises when I attempt to access the 'subscribe' method - within the 'subscribe', the object is of type 'Coupon', but outside of it, the object becomes 'undefined'.
Is there a way for me to maintain the type of the object outside of the 'subscribe' method and use it in other components?
log - 1 :
company-coupons.component.ts:24 **undefined**
log - 2 :
company.service.ts:32
**Coupon {id: 44, title: "Test", startDate: "2000-01-01", endDate: "2000-02-02", category: 3, …}**
fetchAllCoupons() {
this.storagService.getCompanyCoupons().subscribe(
coupons => {
this.coupons = coupons
this.coupon = new Coupon(
coupons[0].amount,
coupons[0].category,
coupons[0].endDate,
coupons[0].id,
coupons[0].imageURL,
coupons[0].price,
coupons[0].startDate,
coupons[0].title,
)
console.log(this.coupon);
})
}
getCompanyCoupons(): Observable<Coupon[]> {
const params = new HttpParams()
.set("token", this.token)
return this.http.get<Coupon[]>('http://localhost:8080/api/company/get_company_coupons', { params })
}
getCoupons() {
this.fetchAllCoupons()
return this.coupons
}
EDIT: To clarify my question further, I am facing the issue of the object being 'undefined' only outside of the 'subscribe' method. I am looking for a solution to maintain its type outside the subscribe in other components.