I'm trying to get Coupon object from the server (REST API + Angular) ,
When im using 'subscribe' - inside the 'subscribe' the object from type 'Coupon', But outside of the subscribe It's becoming 'undefined'.
What do I have to do to keep his type outside the subscribe in another 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: I want to clarify my question. Only out of ths 'subscribe' im getting 'undefined', and i trying ti keep his type outside the subscribe in another components