I have a TypeScript class named User with various properties such as id, userName, age, and more. The class also includes a fullName method that combines the givenname and surname of the user. Additionally, there is a sayHello function that logs a greeting message.
export class User {
id: number;
userName: string;
knownAs: string;
age: number;
gender: string;
created: Date;
lastActive: Date;
photoUrl: string;
city: string;
country: string;
surname: string;
givenname: string;
get fullName(): string {
return `${this.givenname} ${this.surname}`;
}
sayHello() {
console.log(`Hello, my name is ${this.surname} ${this.givenname}!`);
}
}
Next, I have a service function:
user: User;
this.userService.getUser(this.loggedUser.nameid).subscribe((user: User) => {
this.user = user;
this.user.givenname = this.loggedUser.given_name;
this.user.surname = this.loggedUser.family_name;
console.log(this.user.fullName);
this.user.sayHello();
});
However, when checking the console for the results, I encountered an issue where console.log(this.user.fullName) returned 'undefined' and this.user.sayHello() threw an error 'TypeError: Object doesn't support property or method 'sayHello'. How can I access the properties and functions defined within the User class once I retrieve the data from the server?