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

238
Views
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 answers
Answer question

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 Report
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!