2011-06-30 60 views
5

我使用Node.js的模塊node_redis:如何將哈希映射數組插入到Redis中? (Node.js的)

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); 

當我做set,我得到一個錯誤,爲什麼呢?

{ 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 

回答

10

set方法需要一個字符串作爲第二個參數。

你可以字符串化的data變量,即

rclient.set("mykey", JSON.stringify(data)) 
4
  • 您可以將其編碼成JSON(JSON.stringify),然後在Redis的插入。要解碼你然後使用JSON.parse
  • Redback在node_redis之上有一些很好的抽象。 Hash可能是你在找什麼?