skip to Main Content
const updateLeaderboards = async () => {
  try {
    const users = await User.findAll();
    const leaderboardData = [];

    users.forEach((user) => {
      // Add user data to the leaderboardData array in the required format
      leaderboardData.push(user.totalAmount);
      leaderboardData.push(user.username);
    });

    // Store the leaderboard data as a Sorted Set in Redis
    const redisArgs = ['leaderboard', ...leaderboardData];
    console.log("redisargs #######", redisArgs);
      client.zAdd( JSON.stringify(redisArgs), (err, response) => {
      if (err) {
        console.error('An error occurred while updating leaderboard data:', err);
      } else {
        console.log('Leaderboard updated successfully in Redis.');
      }
    });
  } catch (error) {
    console.error('An error occurred while updating leaderboard data:', error);
  }
};
redisargs ####### [ 'leaderboard', 0, 'ranjith', 500, 'vinnu', 0, 'mohan', 80, 'vinay' ]
......../@redis/client/dist/lib/commands/generic-transformers.js:51

           *return num.toString();*
                       ^

**TypeError: Cannot read properties of undefined (reading 'toString')**
    at transformNumberInfinityArgument 
..../node_modules/@redis/client/dist/lib/commands/generic-transformers.js:51:24)

Why am getting this error

2

Answers


  1. In leaderboardData, you have a mix of integer and string. You need to convert your user.totalAmount into string and it will work

    Login or Signup to reply.
  2. The client zAdd function expects an object or array of objects with score and value (see here). In your case redisArgs is an array

    It should be like below:

    await client.zAdd('key', { score: 1, value: 'value' })
      .zAdd('key', [{ score: 1, value: '1' }, { score: 2, value: '2' }])
      .exec()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search