Question

I'm trying to use the ZINTERSTORE command of redis from node.js using node_redis:

//node.js server code
var redis = require("redis");
var client = redis.createClient();

// ... omitted code ...

exports.searchImages = function(tags, page, callback){

  //tags = ["red", "round"]

  client.ZINTERSTORE("tmp", tags.length, tags.join(' '), function(err, replies){

    //do something

  });
}

But the call client.ZINTERSTORE throws the error: [Error: ERR syntax error]. Passing in tags as an array (instead of using tags.join(' ')) throws the same error.

Where can I find the correct syntax for this command? The source code for node_redis has it buried in the javascript parser, but it's tricky to see what's going on without 'stepping through' the code. Is there a good way to do step through debugging with node.js?

Était-ce utile?

La solution

There are multiple ways to debug a Redis client with node.js.

First you can rely on the Redis monitor feature to log every commands received by the Redis server:

> src/redis-cli monitor
OK
1371134499.182304 [0 172.16.222.72:51510] "info"
1371134499.185190 [0 172.16.222.72:51510] "zinterstore" "tmp" "2" "red,round"

You can see the zinterstore command received by Redis is ill-formed.

Then, you can activate the debugging mode of node_redis by adding the following line in your script:

redis.debug_mode = true;

It will output the Redis protocol at runtime:

Sending offline command: zinterstore
send ncegcolnx243:6379 id 1: *4
$11
zinterstore
$3
tmp
$1
2
$9
red,round

send_command buffered_writes: 0  should_buffer: false
net read ncegcolnx243:6379 id 1: -ERR syntax error

Then, you can use node.js debugger. Put a debugger breakpoint in the code in the following way:

function search(tags, page, callback) {
  debugger;    // breakpoint is here
  client.ZINTERSTORE("tmp", tags.length, tags, function(err, replies){
    console.log(err);
    console.log(replies);
    callback('ok')
  });
}

You can then launch the script with node in debug mode:

$ node debug test.js
< debugger listening on port 5858
connecting... ok
break in D:\Data\NodeTest\test.js:1
  1 var redis = require("redis");
  2 var client = redis.createClient( 6379, "ncegcolnx243" );
  3
debug> help
Commands: run (r), cont (c), next (n), step (s), out (o), backtrace (bt), setBreakpoint (sb), clearBreakpoint (cb),
watch, unwatch, watchers, repl, restart, kill, list, scripts, breakOnException, breakpoints, version

debug> cont
break in D:\Data\NodeTest\test.js:8
  6 function search(tags, page, callback) {
  7
  8   debugger;
  9   client.ZINTERSTORE("tmp", tags.length, tags, function(err, replies){
 10     console.log(err);

... use n(ext) and s(tep) commands ...

By stepping through the code, you will realize that the command array is not correct because the tags are serialized and processed as a unique parameter.

Changing the code as follows will fix the problem:

var cmd = [ "tmp", tags.length ];
client.zinterstore( cmd.concat(tags), function(err, replies) {
    ...
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top