Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda