如何在nodejs(nowjs)中处理hgetall()的结果?
我使用的是redis + nowjs。 我想知道如何处理 hgetall() 的结果? 当我尝试在客户端显示“结果”时,我只得到[object Object](它可能是来自服务器端js的字符串)。
//Redis 结果是
redis> HSET cards:lightning-bolt name "Lightning Bolt"
(integer) 1
redis> HSET cards:lightning-bolt text "Blah blah blah"
(integer) 1
redis> HGETALL cards:lightning-bolt
1) "name"
2) "Lightning Bolt"
3) "text"
4) "Blah blah blah"
redis>
//在我的服务器 js 中
everyone.now.signalShowRedisCard = function(card_id) {
var self = this;
client.hgetall(("cards:%s" % card_id), function (err, res) {
nowjs.getGroup(self.now.room).now.receiveShowRedisCard(res);
});
}
//在我的客户端 js 中(警报仅输出 [object Object])
now.receiveShowRedisCard = function(card_data) {
alert("redis card: "+card_data);
try {
console.log('card data: '+ card_data);
} catch(e) {
console.log("Firebug not installed.");
}
}
有什么想法吗?任何答复表示赞赏。
I am using redis + nowjs.
I would like to know how to go about handling a result from an hgetall()?
When I try to display the "result" in the client side, I only get [object Object] (it is probably a string from the server-side js).
//Redis result is
redis> HSET cards:lightning-bolt name "Lightning Bolt"
(integer) 1
redis> HSET cards:lightning-bolt text "Blah blah blah"
(integer) 1
redis> HGETALL cards:lightning-bolt
1) "name"
2) "Lightning Bolt"
3) "text"
4) "Blah blah blah"
redis>
//In my server js
everyone.now.signalShowRedisCard = function(card_id) {
var self = this;
client.hgetall(("cards:%s" % card_id), function (err, res) {
nowjs.getGroup(self.now.room).now.receiveShowRedisCard(res);
});
}
//In my client js (the alert just outputs [object Object])
now.receiveShowRedisCard = function(card_data) {
alert("redis card: "+card_data);
try {
console.log('card data: '+ card_data);
} catch(e) {
console.log("Firebug not installed.");
}
}
Any ideas? Any reply is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当使用 hgetall 时,你会得到一个对象数组。根据具体情况,可以这样处理:
when using hgetall you get an array of objects back. Depending on the scenario it could be handled like this:
来自node_redis自述文件:
我不知道 nowjs 到底是如何处理传递 JavaScript 对象的,但是您可以尝试对从
hgetall
返回的res
进行JSON.stringify
服务器端,然后查看客户端是否收到 JSON 字符串。如果是,那么只需将其解析回 JavaScript 对象,以便在客户端使用它。From node_redis readme:
I don't know how exactly is nowjs handling passing JavaScript objects, but you can try to
JSON.stringify
theres
returned from thehgetall
on the server side and then see if you get JSON string on the client. If yes then just parse it back to JavaScript object in order to work with it on the client side.