Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
¿Cómo mantener una instancia de una Clase después de JSON.Stringified?

tengo esta clase

 class SpaceSession { constructor(session) { this.sess = session; } setUserId(userId) { // ... } static async getUserSpaces(userId) { // ... } setSessionSpace(accessToken, spaceIds) { // ... } }

Quiero almacenarlo en el caché de Redis, así que necesito serializarlo en una cadena con JSON.Stringify()

 const newSession = new Session(req.session); newSession.userId = user.id; storeCacheObject('cache_key', JSON.stringify(newSession));

Pero después de obtener el valor de caché, el objeto ya no es una instancia de la clase de sesión, hace que no pueda acceder a las funciones.

He hecho el enfoque de reemplazo de JSON.Stringify

 JSON.stringify(newSession, (key, value) => { if (typeof value === 'function') { return value.toString(); } return value; }),

Y al enfoque JSON

 class SpaceSession { // just like previous SpaceSession class that I show to you above // ... toJson() { return { setUserId: this.setUserId, getUserSpaces: this.getUserSpaces, setSessionSpace: this.setSessionSpace, }; } }

Pero ninguno de ellos funciona para mí.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!