I've been over this for a few days now, but I simply don't understand what's happening:
I have a Serializable
class with 2 main methods, serialize()
which just stringifies the instance and wake(serialized)
which should wake up or "resurrect" an instance.
class Serializable {
constructor() {}
serialize() {
return JSON.stringify(this);
}
wake(serialized) {
// this is probably where the problem is and I don't really know how to approach it
const unserialize = JSON.parse(serialized)
return new this.constructor(unserialize)
}
}
Then I declare User
which extends from Serializable
:
class User extends Serializable {
constructor({
firstName,
lastName,
}) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
}
I create a new User
:
let newUser = new User({
firstName: "Solid",
lastName: "Snake",
})
I serialize newUser
and for example clarity I delete the instance:
const serialized = newUser.serialize()
newUser = null
Now I want to wake up the instance using the serialized
constant:
const wakeUp = new User.wake(serialized)
This is where it fails. Error in Quokka is Cannot destructure property 'firstName' of 'undefined' as it is undefined.
Error in console is the TypeError
from the title.