Hey I am getting this error
/home/gm/Desktop/ZulfDemo/streamusingws/node_modules/@redis/client/dist/lib/client/RESP2/encoder.js:17
throw new TypeError('Invalid argument type');
While I am trying to run this script
const redis = require("redis");
const redisclient = redis.createClient();
(async () => {
await redisclient.connect();
})();
console.log("Connecting to the Redis");
redisclient.on("ready", () => {
console.log("Connected!");
});
redisclient.on("error", (err) => {
console.log("Error in the Connection");
});
const generateRandomNumber = () => {
const randomNumber = Math.floor(Math.random() * 100000); // generate random number between 0 and 100,000
console.log('Generated random number:', randomNumber); // add this line to check the value of randomNumber
redisclient.RPUSH(['randomNumbers', randomNumber], (err, reply) => { // store number in Redis list
if (err) {
console.log('Error in rPush:', err); // add this line to log any errors
} else {
console.log('Stored ' + randomNumber + ' in Redis list');
}
});
}
setInterval(generateRandomNumber, 1000); // generate a random number every second
Here i tried with rpush, rPush and all but still getting this error can anyone help m to solve this.
2
Answers
So finally got the answer here we have to pass string in all the terms instead of number so I just add toString() after number like this.
You don’t need to put the
RPUSH
parameters in an array. Also with node-redis 4 you don’t need to use callbacks, you should use promises instead.Here’s a cleaned up version that shows both of these things:
Output from running this:
If you use a module, you won’t need the IIFE either as you can use top level
await
if your Node version is newer. So this could be further simplified by adding"type":"module"
in yourpackage.json
: