skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.

    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.toString()], (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');
          }
        });
      }
    

  2. 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:

    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 = async () => {
        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
        try {
          await redisclient.RPUSH("randomNumbers", `${randomNumber}`);
        } catch (e) {
          console.log("Error pushing to Redis:");
          console.log(e);
        }
      }
      
      setInterval(generateRandomNumber, 1000);
    

    Output from running this:

    $ node index.js
    Connecting to the Redis
    Connected!
    Generated random number: 69258
    Generated random number: 15338
    Generated random number: 45459
    Generated random number: 79371
    Generated random number: 89456
    Generated random number: 60289
    ^C
    $ redis-cli
    127.0.0.1:6379> lrange randomNumbers 0 -1
    1) "69258"
    2) "15338"
    3) "45459"
    4) "79371"
    5) "89456"
    6) "60289"
    

    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 your package.json:

    import { createClient } from "redis";
    const redisclient = createClient();
    
    await redisclient.connect();
    console.log("Connected to Redis.");
    
    const generateRandomNumber = async () => {
      const randomNumber = Math.floor(Math.random() * 100000);
      console.log("Generated random number:", randomNumber);
      try {
        await redisclient.RPUSH("randomNumbers", `${randomNumber}`);
      } catch (e) {
        console.log("Error pushing to Redis:");
        console.log(e);
      }
    }
      
    setInterval(generateRandomNumber, 1000);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search