在我的节点应用程序中,我使用redis DB来存储数据。使用键获取存储值时,我没有得到预期的输出。在redis中如何获取特定键的值?
var redis=require('redis');
var client = redis.createClient();
var pageContent={a: "a", b: "b", c: "c"};
//client.set('A',pageContent);//here i'm setting the value
client.get('A',function(err,res){
if(!err){
Object.keys(res).forEach(function(k){
console.log('key is '+k + ' value is '+res[k]);
});
}
else{
console.log('error');
}
});
上面的代码是不给存储value.While循环,我发现了以下错误
TypeError: Object.keys called on non-object
这就是我一直res.toString()的结果;但我没有得到存储的价值instaed,我只得到[对象对象];
您只能使用'SET'存储字符串。因此,无论是保存时保存的对象是JSON.stringify',还是读取时的JSON.parse',*或*都使用['HMSET']将其存储为散列值(http://redis.io/commands/ hmset)和['HGETALL'](http://redis.io/commands/hgetall)。 –
@LinusGThiel谢谢你..我会用hmset和hgetall – sachin
酷 - 你知道吗,我会把它作为答案。 –