I have the following typescript model object user
export class User {
constructor(
private _name: string,
private _email: string
) {}
public get name():string {
return this._name;
}
public set name(value:string) {
this._name = value;
}
get email():string {
return this._email;
}
set email(value:string) {
this._email = value;
}
}
I store the object via this code:
let user = new User('myName', 'myEmail');
localStorage.setItem('user', JSON.stringify(user));
If I look into the local storage there is the following string:
{"_name":"myName","_email":"myEmail"}
How do I get the user object again?
let user: User = JSON.parse(localStorage.getItem('user'));
console.log(user.name); // logs undefined. Should log 'myName'
console.log(user._name); // this logs 'myName'. But according to the typescript documentation this should NOT work!
I guess this has something to to with the underscores the object is stored with. How can I receive the object correctly?