Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
Is there any way that I don't use duplicate(or copy) variables for redisJSON?

Basically, I am trying to store a JSON of my application in redis using redisJSON. And I have different functions here and there in the project where I modify and update the json. Now currently, when I tried storing and updating the values in redis, I have to continuously duplicate the values to make changes and then update it back to redis. Is there any alternative for this procedure?

const addUser = async () => {
  try {
    const USERS_KEY = 'users';
    let mainUsers;
    mainUsers = await redisClient.json.get(USERS_KEY, {
      path: '.',
    });

    if (!mainUsers) {
      await redisClient.json.set(USERS_KEY, '.', {});
      mainUsers = await redisClient.json.get(USERS_KEY, {
        path: '.',
      });
    }

    const mainUsersCopy = { ...mainUsers };
    console.log('The actual object we need', mainUsersCopy);

    const newUser = {
      socketId: 123,
      roomId: 456,
      teamName: teamA,
      rank: 69,
      userName: 'X'
    };

    mainUsersCopy[userName] = newUser;

    await redisClient.json.set(USERS_KEY, '.', mainUsersCopy);

    return { status: 2, userObj: mainUsersCopy[userName] };
  } catch (err) {
    console.log(err);
  }
}; 

I have given an example of above and based on that can you guys provide me a solution for not using mainUsersCopy. FYI, console logging the mainUsers we get:

mainUsers is here [object Object]

So that is why the mainUsers has been destructured. Thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You shouldn't need to copy anything here. Just set the value within the JSON document that you want to set using .set:

const newUser = {
  socketId: 123,
  roomId: 456,
  teamName: teamA,
  rank: 69, /* nice */
  userName: 'X'
};

await redisClient.json.set(USERS_KEY, `.${newUser.userName}`, newUser);

This should set the object newUser within the existing the JSON document, creating a new key in it called, in this example X.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda