I've encountered an issue while trying to access some methods of my model object as it keeps returning an error stating that the function does not exist.
Below is the model class I have created :
class Expense {
private name: string;
private title: string;
private amount: number;
private description: string;
private date: Date;
private status: string;
public formatetdDate: string;
private _id : string;
constructor(name: string, title: string, amount: number, description: string, date: Date, status: string) {
this.name = name;
this.title = title;
this.amount = amount;
this.description = description;
this.date = new Date(0);
this.status = status;
this.formatetdDate = this.dateFormat();
}
public getDate(): Date {
return this.date;
}
public dateFormat(): string {
let dd = this.date.getDate();
let mm = this.date.getMonth() + 1;
const yyyy = this.date.getFullYear();
if (dd < 10) {
dd = +`0${dd}`;
}
if (mm < 10) {
mm = +`0${mm}`;
}
console.log(dd + '/' + mm + '/' + yyyy);
return dd + '/' + mm + '/' + yyyy;
}
}
export default Expense;
Currently, I am fetching data from the server using http requests and mapping it to this model :
this.expenseService.getExpensesList().subscribe((expenses) => {
if (expenses.success) {
this.expensesList = expenses.data as Expense[];
this.total = +expenses.total;
this.pages = +expenses.pages;
this.limit = +expenses.limit;
} else {
alert(expenses.message);
}
});
If I attempt to execute the following actions :
this.expensesList[0].getData()
or
this.expensesList[0].dateFormat()
an error is consistently thrown mentioning that these functions do not exist. However, when attempting to print the date with (this.expensesList[0].date), an error occurs due to access violation (private).
Can anyone point out what mistake I might be making?
Thank you