Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I insert an array of hash maps into Redis? (Node.js)

I'm using the Node.js module node_redis:

var data = [ {'name':'matt', 'id':'333' } , {'name':'Jessica','id':'492'} ] ;

//Initialize Redis
var redis = require('redis'),
rclient = redis.createClient(settings.redis.port, settings.redis.host,{pass:settings.redis.password});
rclient.auth(settings.redis.password, function(){});
rclient.on("error", function (err) {});


//OK, insert the data into redis
rclient.set("mykey", data); 

When I do set, I get an error, why?

{ stack: [Getter/Setter],
  arguments: undefined,
  type: undefined,
  message: 'ERR wrong number of arguments for \'set\' command' }
Error: ERR wrong number of arguments for 'set' command
like image 543
TIMEX Avatar asked Jun 30 '11 01:06

TIMEX


2 Answers

The set method expects a string as the second argument.

You could stringify your data variable, i.e.

rclient.set("mykey", JSON.stringify(data))
like image 182
James Avatar answered Nov 02 '22 23:11

James


  • You could encode it to JSON(JSON.stringify) and then insert it in redis. To decode you then use JSON.parse
  • Redback has some nice abstractions on top of node_redis. Hash might be what you are looking for?
like image 24
Alfred Avatar answered Nov 02 '22 23:11

Alfred