在我的節點應用程序中,我使用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
酷 - 你知道嗎,我會把它作爲答案。 –