salep
salep

Reputation: 1380

node_redis ZRANGEBYLEX returns "ERR min or max not valid string range item" error

Solved. Solution is below, thanks to @hobbs, I corrected it.

I'm using node_redis package.

Here's how I use ZRANGEBYLEX command via cli.

127.0.0.1:6379> zadd movie 0 "Creed:2015:id1"
(integer) 1
127.0.0.1:6379> zadd movie 0 "Secret in Their Eyes:2015:id2"
(integer) 1
127.0.0.1:6379> zadd movie 0 "Spotlight:2015:id3"
(integer) 1
127.0.0.1:6379> zadd movie 0 "The Lobster:2015:id4"
(integer) 1
127.0.0.1:6379> zadd movie 0 "The Corpse of Anna Fritz:2015:id5"
(integer) 1
127.0.0.1:6379> zadd movie 0 "The Martian:2015:id6"
(integer) 1
127.0.0.1:6379> zadd movie 0 "The Revenant:2015:id7"
(integer) 1
127.0.0.1:6379> zadd movie 0 "Steve Jobs:2015:id8"
(integer) 1

Getting all movies that start with "The" word.

127.0.0.1:6379> zrangebylex movie "[The" "(The\xff"
1) "The Corpse of Anna Fritz:2015:id5"
2) "The Lobster:2015:id4"
3) "The Martian:2015:id6"
4) "The Revenant:2015:id7"

So far, so good.

First 2 items.

zrangebylex movie "[The" "(The\xff" limit 0 2
1) "The Corpse of Anna Fritz:2015:id5"
2) "The Lobster:2015:id4"

Here's my attempt to do the same thing with Node.js, but I get an error.

var redis = require("redis");
var client = redis.createClient();

app.get('/redis', function(req, res) {
  var args2 = ["movie", "The", "The", 'LIMIT', 0, 2];
  client.zrangebylex(args2, function(err, response) {
    if (err) throw err;
    console.log(response);
  });
});

Here's the full error log.

Error: ERR min or max not valid string range item
www-0     at Error (native)
www-0     at HiredisReplyParser.return_data (/Users/me/Desktop/project/node_modules/redis/lib/parsers/hiredis.js:14:28)
www-0     at HiredisReplyParser.execute (/Users/me/Desktop/project/node_modules/redis/lib/parsers/hiredis.js:24:22)
www-0     at Socket.<anonymous> (/Users/me/Desktop/project/node_modules/redis/index.js:131:27)
www-0     at emitOne (events.js:77:13)
www-0     at Socket.emit (events.js:169:7)
www-0     at readableAddChunk (_stream_readable.js:146:16)
www-0     at Socket.Readable.push (_stream_readable.js:110:10)
www-0     at TCP.onread (net.js:523:20)

SOLUTION

app.get('/redis', function(req, res) {
  var args2 = ["movie", "[The", '(The\xff', 'LIMIT', "0", "2"];
  client.zrangebylex(args2, function(err, response) {
    if (err) throw err;
    console.log(response);
    res.end();
  });
});

Upvotes: 0

Views: 3266

Answers (2)

salep
salep

Reputation: 1380

@hobbs is right. The solution is below.

app.get('/redis', function(req, res) {
  var args2 = ["movie", "[The", '(The\xff', 'LIMIT', "0", "2"];
  client.zrangebylex(args2, function(err, response) {
    if (err) throw err;
    console.log(response);
    res.end();
  });
});

Upvotes: 0

hobbs
hobbs

Reputation: 239841

You're not sending the same arguments — in particular your range doesn't have the "(" or "[" characters that are required for the limits of ZRANGEBYLEX. You're also not including the \xff on the upper limit.

Upvotes: 2

Related Questions