Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
node.js如何在中间件中从redis数据库中获取数据_Node.js_Redis - Fatal编程技术网

node.js如何在中间件中从redis数据库中获取数据

node.js如何在中间件中从redis数据库中获取数据,node.js,redis,Node.js,Redis,我正在做一个中间件模块,它将从redis中提取数据,并将数据放到req.my_会话中 这是在app.use()内部调用的函数 我认为这个问题是因为async in-loop,您可以尝试以下方法 function parse_cookies(req){ if(req.headers.cookie != null){ var result = req.headers.cookie.match(new RegExp('m:[^=]*=[^; ]*', 'ig'));

我正在做一个中间件模块,它将从redis中提取数据,并将数据放到req.my_会话中

这是在app.use()内部调用的函数


我认为这个问题是因为async in-loop,您可以尝试以下方法

  function parse_cookies(req){
      if(req.headers.cookie != null){
        var result = req.headers.cookie.match(new RegExp('m:[^=]*=[^; ]*', 'ig'));
        if(result != null){
          var promises = [];
          for(var i = 0; i < result.length; i++){
            var result1 = result[i].split('=');
            promises.push(getFromRd(req, result1));

          }

          return Promise.all(promises)
                 .then(() => {
                      return next()
                 })
                 .catch((e) => {
                      return next({error: e})
                 })
        }
      }
    } // parse_cookies


  function getFromRd(req, result1) {
         req.my_session[result1[0].substr(2)] = result1[1];
         // get from redis value
         return client.get('sess:'+result1[1], function(err, reply) {
                if (err) {
                    throw Error(' failed to find ' + 'sess:' + result1[1])
                }
                // reply is null when the key is missing
                console.log(reply);
                let li = i;
                req.my_session[result1[0].substr(2)] = reply;
                console.log('li = ' + li);
                console.log('result1.lenght= ' + result.length);
                return {success:true}
            });
  }
函数解析\u cookies(请求){
if(req.headers.cookie!=null){
var result=req.headers.cookie.match(新的RegExp('m:[^=]*=[^;]*','ig');
如果(结果!=null){
var承诺=[];
对于(变量i=0;i{
返回下一个()
})
.catch((e)=>{
返回下一个({错误:e})
})
}
}
}//解析cookies
函数getFromRd(请求,结果1){
请求我的会话[result1[0].substr(2)]=result1[1];
//从redis值中获取
返回client.get('sess:'+result1[1],函数(err,reply){
如果(错误){
抛出错误('未能找到'+'sess:'+result1[1])
}
//当密钥丢失时,reply为null
控制台日志(应答);
设li=i;
请求我的会话[result1[0].substr(2)]=回复;
console.log('li='+li);
log('result1.lenght='+result.length');
返回{success:true}
});
}
problem it's get data from database in my middleware, I can't because redis has callback function

    client.get("missingkey", function(err, reply) {
    // reply is null when the key is missing
    console.log(reply);
});
  function parse_cookies(req){
      if(req.headers.cookie != null){
        var result = req.headers.cookie.match(new RegExp('m:[^=]*=[^; ]*', 'ig'));
        if(result != null){
          var promises = [];
          for(var i = 0; i < result.length; i++){
            var result1 = result[i].split('=');
            promises.push(getFromRd(req, result1));

          }

          return Promise.all(promises)
                 .then(() => {
                      return next()
                 })
                 .catch((e) => {
                      return next({error: e})
                 })
        }
      }
    } // parse_cookies


  function getFromRd(req, result1) {
         req.my_session[result1[0].substr(2)] = result1[1];
         // get from redis value
         return client.get('sess:'+result1[1], function(err, reply) {
                if (err) {
                    throw Error(' failed to find ' + 'sess:' + result1[1])
                }
                // reply is null when the key is missing
                console.log(reply);
                let li = i;
                req.my_session[result1[0].substr(2)] = reply;
                console.log('li = ' + li);
                console.log('result1.lenght= ' + result.length);
                return {success:true}
            });
  }