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
In leaderboardData, you have a mix of integer and string. You need to convert your
user.totalAmount
into string and it will workThe client zAdd function expects an object or array of objects with score and value (see here). In your case
redisArgs
is an arrayIt should be like below: