node-redis icon indicating copy to clipboard operation
node-redis copied to clipboard

sMembers error = undefined

Open xantos008 opened this issue 2 years ago • 5 comments

Hi guys. Need quick help. After i'm doing sadd and print in command promt redis-cli smembers <MyKey> it works But! When i'm trying await redisClient.sMembers('MyKey') if always undefiend. How can i get value of my key?

Environment:

  • Node.js Version: v16.14.2
  • Redis Server Version: Redis server v=3.0.504 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=a4f7a6e86f2d60b3
  • Node Redis Version: "redis": "^4.0.6"
  • Platform: Windows 10, Mac OS 11.6 -->

xantos008 avatar Apr 30 '22 11:04 xantos008

This code does not reproduce it, am I missing something?

import { createClient } from '@node-redis/client';

const client = createClient();

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

await client.connect();

console.log(await client.sMembers('key')); // []
await client.sAdd('key', 'member');
console.log(await client.sMembers('key')); // ['member']

leibale avatar Apr 30 '22 14:04 leibale

Hi, I am still having this issue as of 8/5/2022. It would be great if we could raise the issue. Thanks.

PopBot avatar Aug 05 '22 20:08 PopBot

@PopBot without reproducing it I won't be able to solve it. See my last comment please.

leibale avatar Aug 05 '22 20:08 leibale

I just found the cause of the "error". It is the fault of the documentation for the library. For being such a popular library—and the fact that Node Redis is relied upon by thousands of developers per week—I am honestly surprised the documentation is so poor. I might submit a PR this week with suggestions to update the docs.

It's not documented anywhere, but use client.sMembers(key, (_, values) => {}). The first callback return value is the error, and the second is the Object of the set members values.

PopBot avatar Aug 06 '22 23:08 PopBot

@PopBot you are reading the v4 documentation and using a v3 client. BTW, this issue is refereeing to v4, see the end of the first comment in the thread. https://github.com/redis/node-redis/tree/v3.1.2

leibale avatar Aug 06 '22 23:08 leibale

I'm experiencing this issue as well. When using [email protected] in legacy mode, sAdd and sMembers always return undefined. Once I disable legacy mode, it works as expected. I'm using Node v16.18.1 with redis 6.2.7.

import { createClient } from "redis";

const client = createClient({
  url: "MY_URL",
  // immediately throw error for failed requests
  disableOfflineQueue: true,
  // required for connect-redis@6
  legacyMode: true,
});

client.on("error", (error) => {
  console.error(error);
});

await client.connect();

const key = "item.1";
console.log("before", await client.sMembers(key)); // undefined
console.log("resp", await client.sAdd(key, "true")); // undefined
console.log("session list after", await client.sMembers(key)); // undefined

Piccirello avatar Nov 28 '22 15:11 Piccirello

Shortly after posting the above, I figured out my issue. The migration guide mentions that the v4 interface is still accessible using client.v4, while I was only using client. Switching to client.v4 fixed the issue (example below). I'm not sure that the typing of the client is correct when using legacy mode, as it still supplies the v4 interfaces.

import { createClient } from "redis";

const client = createClient({
  url: "MY_URL",
  // immediately throw error for failed requests
  disableOfflineQueue: true,
  // required for connect-redis@6
  legacyMode: true,
});

client.on("error", (error) => {
  console.error(error);
});

await client.connect();

const key = "item.1";
console.log("before", await client.v4.sMembers(key)); // []
console.log("resp", await client.v4.sAdd(key, "true")); // 0
console.log("session list after", await client.v4.sMembers(key)); // ['true']

Piccirello avatar Nov 28 '22 15:11 Piccirello