2

I've just installed a redis client for my node application. Package information is as follows:

me@mydevbox:/var/www/html/node/test$ cat package.json 
{
  "name": "test",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.13.2",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "ejs": "~2.3.3",
    "express": "~4.13.1",
    "morgan": "~1.6.1",
    "redis": "^2.6.2",
    "serve-favicon": "~2.3.0"
  }
}

I need to run the following command in my node js application:

127.0.0.1:6379> SCAN 0 match widget:914*
1) "0"
2) 1) "widget:9145090003_13:00_17:00"
   2) "widget:9145090003_00:00_00:00"
   3) "widget:9145090003_08:00_12:00"
127.0.0.1:6379> 

But I've been reading the docs and it's not clear how to do this. So far, using the samples, I've written the following code:

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

client.on('error', function (err) {
  console.log('Error ' + err);
});

But i haven't found a generic method that allows me to send various Redis commands. This is the reference that I'm using:

https://www.npmjs.com/package/redis

I'm currently googling around to see what other references I can find... but any suggestions would be appreciated.

Thanks

EDIT 1

I've tried the send_command, and I'm getting something back... but I don't quite know how to query the object. It feels as though it's empty...

Here's what I've tried:

client.send_command("SCAN", [0,"MATCH", "WIDGET:914*"], function(err, reply) {

        console.log(reply[0].length);
        console.log(reply[1].length);

});

That returns:

me@mydev:/var/www/html/node/test$ DEBUG=test:* npm start

> test@0.0.0 start /var/www/html/node/test
> node ./bin/www

attempting to scan ...
  translatedid:server Listening on port 3000 +0ms
1
0

I also tried this:

client.send_command("SCAN", [0,"MATCH", "WIDGET:914*"], function(err, reply) {
    for (var i = 0; i < reply.length; i++) {
        console.log("variable type for item " + i + " is: " + typeof(reply));
        console.log("properties for item " + i + " are: " + Object.getOwnPropertyNames(reply[i]).sort());

        }

 });

Which returns:

> test@0.0.0 start /var/www/html/node/test
> node ./bin/www

attempting to scan ...
  test Listening on port 3000 +0ms
variable type for item 0 is: object
properties for item 0 are: 0,length
variable type for item 1 is: object
properties for item 1 are: length

But I can't seem to figure out how to get at my keys.

EDIT 2

I had to change this:

 client.send_command("SCAN", [0,"MATCH", "WIDGET:914*"], function(err, reply) {

to this:

client.send_command("SCAN", [0,"MATCH", "widget:914*"], function(err, reply) {

Stupid mistake.

Happydevdays
  • 1,982
  • 5
  • 31
  • 57
  • The native `node-redis` package doesn't have a scan method as far as I remember. Theres an NPM package https://www.npmjs.com/package/redis-scanstreams that can do it, or you can use the `send_command` method to manually run a SCAN command. – Sterling Archer Sep 12 '16 at 19:07
  • ok perfect. i'll try the send_command first to see if i can get it working. – Happydevdays Sep 12 '16 at 19:09
  • @SterlingArcher any thoughts on my Edit 1? – Happydevdays Sep 12 '16 at 19:28
  • It looks like there are no replies. Make sure you check for errors (you're not using the error param) – Sterling Archer Sep 12 '16 at 20:46
  • yes, that is the reason for the error but as you can see from the post, the command that I'm using in redlis cli and also in node is the same... so i don't know why i have no results – Happydevdays Sep 12 '16 at 20:59
  • According to my search, it looks like you're not sending the args correctly. I believe they're an array after the command. Try (something like) `client.send_command("SCAN", [0, "MATCH", "WIDGETS:914*], function(err, reply) { ... });` – Sterling Archer Sep 12 '16 at 21:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123255/discussion-between-happydevdays-and-sterling-archer). – Happydevdays Sep 13 '16 at 13:17

0 Answers0