0

i am using rpush method to store the list in the redis and lrange to get the list elements from the redis. In node js i am getting error that rpush and lrange is not function.

So, I use RPUSH AND LRANGE functions according to the redis node documentation. The app runs correctly but both functions are not triggerring. RPUSH functiom is setting the list items in the redis as i can see them in redis-cli but this function doesnot give me any response of data saved and I write in console "Hello from inside RPUSH" it is not printing it in console(function not triggers), but the data is pushing in the list. LRANGE function does not get the list items, but i can see items through redio-cli here is the code Certainly! Here's your code indented by 4 spaces for posting on Stack Overflow:

const redis = require('redis');
const client = redis.createClient({
    host: '127.0.0.1',
    port: 6379
});

client.on('error', err => console.log('Redis Client Error', err));

// Pushing data into the list using rpush
client.rpush("mylist2", "Hello3", (error, reply) => {
    console.log("Hello from inside client .rpush");
    if (error) {
        console.error('Error pushing data:', error);
    } else {
        console.log('Pushed data:', reply);

        // Once data is pushed, perform lRange operation
        performLRANGE();
    }
});

performLRANGE();

function performLRANGE() {
    // Replace 'mylist' with your actual list key
    const listKey = 'mylist2';
    const startIndex = 0;
    const endIndex = -1; // -1 retrieves all elements

    console.log("Hello from inside performLrange function");
    // Using the lrange command
    client.lrange(listKey, startIndex, endIndex, (error, result) => {
        console.log("Hello from inside LRANGE");
        if (error) {
            console.error('Error:', error);
        } else {
            console.log('Elements in the list:', result);
        }

        // Close the Redis connection
        client.quit();
    });
}

Anyone who can help me to solve this issue.I appreciate your help to resolve this issue. Thank you for your time

1 Answers1

0

Node-Redis 4 onwards use promises rather than callbacks. This was a breaking change in the Node-Redis 4 release but makes your code a lot easier to follow.

Here's an example showing your code refactored to use async/await, I also made it a module so you can use top level async/await and import, but those aren't necessary when working with Node-Redis 4...

package.json:

{
  "name": "solistnode",
  "version": "1.0.0",
  "main": "index.js",
  "type": "module",
  "dependencies": {
    "redis": "^4.6.7"
  }
}

index.js:

import { createClient } from 'redis';

const client = createClient({
  host: '127.0.0.1',
  port: 6379
});

// Connect to Redis...
await client.connect();

try {
  // Pushing data into the list using rpush - use await...
  const rpushResponse = client.rPush('mylist2', 'hello3');
  console.log('Pushed data.');

  const lrangeResponse = await client.lRange('mylist2', 0, -1);
  console.log('Data from the list:');
  console.log(lrangeResponse);
} catch (e) {
  console.log('Error working with Redis list:');
  console.log(e);
}

await client.disconnect();

Node Version:

$ node --version
v18.14.2

Run the code a few times:

$ node index.js
Pushed data.
Data from the list:
[ 'hello3' ]
$ node index.js
Pushed data.
Data from the list:
[ 'hello3', 'hello3' ]
$ node index.js
Pushed data.
Data from the list:
[ 'hello3', 'hello3', 'hello3' ]

If you still need to use the old Node-Redis 3 interface from Node-Redis 4, check out the migration and legacy mode documentation: https://github.com/redis/node-redis/blob/master/docs/v3-to-v4.md

Simon Prickett
  • 3,838
  • 1
  • 13
  • 26