How to insert a hash map array in Redis? (Node.js)

I use the Node.js node_redis module:

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
+5
source share
2 answers

The method setexpects a string as the second argument.

You can tune your variable data, i.e.

rclient.set("mykey", JSON.stringify(data))
+10
source
  • You can encode it in JSON ( JSON.stringify) and then paste it in redis. To decode you, useJSON.parse
  • Redback has some nice abstractions on top of node_redis. Hash may be what you are looking for?
+4
source

All Articles