在node js中使用redis的rpush()和lrange()函数出现问题

问题描述 投票:0回答:1

我使用rpush方法将列表存储在redis中,并使用lrange从redis中获取列表元素。在 Node js 中,我收到错误,rpush 和 lrange 不起作用。

因此,我根据redis节点文档使用RPUSH AND LRANGE函数。该应用程序运行正常,但两个功能均未触发。 RPUSH 函数正在设置 redis 中的列表项,因为我可以在 redis-cli 中看到它们,但是这个函数没有给我任何保存的数据响应,我在控制台中写入“Hello from inside RPUSH”,它没有在控制台中打印它(函数不是触发器),但数据正在推送到列表中。 LRANGE 函数无法获取列表项,但我可以通过 redio-cli 查看项目 这是代码 当然!这是缩进 4 个空格的代码,用于在 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();
    });
}

任何可以帮助我解决此问题的人。我感谢您帮助解决此问题。 谢谢您的宝贵时间

node.js redis node-modules node-redis nodejs-server
1个回答
0
投票

Node-Redis 4 及以上版本使用 Promise 而不是回调。这是 Node-Redis 4 版本中的一项重大更改,但使您的代码更容易遵循。

这是一个示例,显示您的代码被重构为使用 async/await,我还将其作为一个模块,以便您可以使用顶级 async/await 和导入,但在使用 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
v18.14.2

运行代码几次:

$ 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' ]

如果您仍然需要使用 Node-Redis 4 中的旧 Node-Redis 3 接口,请查看迁移和遗留模式文档:https://github.com/redis/node-redis/blob/master/docs/ v3-to-v4.md

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.