使用 node_redis 时如何访问传递给 hgetall 的密钥? [英] How can I access the key passed to hgetall when using node_redis?

查看:52
本文介绍了使用 node_redis 时如何访问传递给 hgetall 的密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 node.js (express) 应用程序,我正在使用 node_redis 从我的 redis 数据库中获取所有用户.

redis = 需要redis"客户端 = redis.createClient()client.smembers "users", (err, user_ids) ->结果 = 新数组()对于 user_ids 中的 user_idclient.hgetall user_id, (err, items) ->结果.推送项目如果 user_ids.length 是 results.lengthres.json 结果

这会产生以下结果:

<预><代码>[{"name": "user1",密码":秘密"},{"name": "user2",密码":秘密"}]

现在我想将 user_id 添加到用户结果中,这样我就可以得到以下输出:

<预><代码>[{用户:1":{"name": "user1",密码":秘密"}},{用户:2":{"name": "user2",密码":秘密"}}]

我遇到的问题是 client.hgetall() 是异步调用的,我不能简单地访问 for 循环中的 user_id.

解决方案

你需要通过在循环中引入一个函数来使用闭包来存储 user_id.一种方法是使用 forEach 函数对数组进行迭代.

这是一个 Javascript 示例:

var redis = require('redis')var client = redis.createClient();函数获取(回调){var 结果 = new Array();client.smembers(用户",函数(错误,用户){if ( users.length == 0 )返回回调(结果);users.forEach(函数(id){client.hgetall(id, function(err,items) {var obj = {};obj[id] = 物品;# 这里可以访问 id 因为它是闭包的一部分结果.推(对象);if ( results.length == users.length ) {回调(结果);}});});});}获取(功能(结果){console.log(JSON.stringify(results));});

输出为:

[ {"user:2":{"name":"user2","password":"secret2"}},{"user:1":{"name":"user1","password":"secret"}}]

I have a node.js (express) application and I'm using node_redis to get all users from my redis db.

redis = require "redis"
client = redis.createClient()

client.smembers "users", (err, user_ids) ->
  results = new Array()
  for user_id in user_ids
    client.hgetall user_id, (err, items) ->
      results.push items
      if user_ids.length is results.length
        res.json results

This produces the following result:

[
  {
    "name": "user1",
    "password": "secret"
  },
  {
    "name": "user2",
    "password": "secret"
  }
]

Now I want the user_id to be added to the user result, so I can get the following output:

[
  {
    "user:1": {
      "name": "user1",
      "password": "secret"
    }
  },
  {
    "user:2": {
      "name": "user2",
      "password": "secret"
    }
  }
]

The problem I have is that client.hgetall() is called asynchronously and I can't simply access the user_id in the for loop.

解决方案

You need to use a closure to store user_id by introducing a function in the loop. One way to do it is to use the forEach function to iterate on the array.

Here is an example in Javascript:

var redis = require('redis')
var client = redis.createClient();

function fetch( callback ) {
   var results = new Array();
   client.smembers( "users", function(err,users) {
      if ( users.length == 0 )
         return callback( results );
      users.forEach( function(id) {
         client.hgetall(id, function(err,items) {
            var obj = {};
            obj[id] = items; # here id can be accessed since it is part of the closure
            results.push(obj);
            if ( results.length == users.length ) {
               callback( results );
            }
         });
      });
   });
}

fetch( function(results) {
   console.log(JSON.stringify(results));
});

The output is:

[ {"user:2":{"name":"user2","password":"secret2"}},
  {"user:1":{"name":"user1","password":"secret"}} ]

这篇关于使用 node_redis 时如何访问传递给 hgetall 的密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆