Javascript for nodejs中的循环不使用redis Hexist

Javascript for nodejs中的循环不使用redis Hexist,javascript,node.js,for-loop,redis,Javascript,Node.js,For Loop,Redis,在redis中执行带有Hexist检查的for循环,但我只得到数组中最后一项的响应。我尝试了所有方法,包括async和一些建议的堆栈溢出,但无法获得解决方案,不确定我缺少了什么 代码如下 reply.forEach(function(replyitem,index){ parsedReply = JSON.parse(replyitem); if(checkMsg(parsedReply.msguuid, index) == nu

在redis中执行带有Hexist检查的for循环,但我只得到数组中最后一项的响应。我尝试了所有方法,包括async和一些建议的堆栈溢出,但无法获得解决方案,不确定我缺少了什么

代码如下

    reply.forEach(function(replyitem,index){
            parsedReply = JSON.parse(replyitem);

                if(checkMsg(parsedReply.msguuid, index) == null){
                    result.push(parsedReply);
                }
            count++;
        if ((count + 1) == reply.length){
        // Pass the info list to the view
                cd("# of info shown when page is loaded: - " + result.length);
                sendAllLocations(whorequested + ":mylocation", res, result);   
            }
        });



 function checkMsg(msgidVal, index){
                redisClient.hexists(blockedlistname, msgidVal, function (rediserr, exists) {
                if (rediserr) {
                    ce("Error  " + JSON.stringify(rediserr));
                } else if (!exists) {
                    return false;
                } else {
                    return true;
                    cd(" not displaying " + msgidVal);
                }
            }); 

 }
您的
checkMsg()
函数正在对redis数据库进行异步调用,但随后尝试从
checkMsg()
函数返回该值。这样不行。异步值不能直接从函数返回。函数在检索异步值之前返回。而且,从异步回调中返回的值只会返回到数据库基础结构,而不会返回到您的任何代码(例如,它没有做任何有用的事情)

您必须以异步方式使用checkMsg编程。它需要一个回调或承诺可以返回结果,然后所有使用checkMsg的代码都必须使用该回调来检查结果。您必须异步使用该函数,就像您在代码中具有异步操作的其他地方所做的那样