I'm not able to access the methods of the class after typecasting json response from the api to the typescript class.
class Stock {
name: String;
purchaseDate: Date;
constructor() {}
convertDates() {
this.purchaseDate = new Date(this.purchaseDate);
}
}
getAll() {
return this.http.get(URL + '/find').map(
(response) => {
this.stocks = response as Array<Stock>;
_.forEach(this.stocks, (stock) => {
stock.convertDates();
}
},
(error) => {
this.stocks = [];
}
);
}
I'm getting an error message as follows: "stock.convertDates is not a function". This works without any error, if I loop through the list of all stocks in the response and create an instance for every stock before calling "convertDates" method. Here is the code for it:
_.forEach(response, (stock) => {
let newstock = new Stock();
_.merge(newstock, stock);
newstock.convertDates();
this.stocks.push(newstock);
});