![](/img/trans.png)
[英]Loop through Redis tables, and get same keys in Nodejs and Expressjs
[英]for loop in nodejs not working with redis hexists
用hexists執行for循環,檢查redis,但是我僅對數組中的最后一項獲得響應。 我嘗試了所有方法,包括異步方法和一些在堆棧溢出中建議的方法,但無法獲得解決方案,不確定我缺少什么。
代碼如下
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的代碼都必須使用該回調來檢查結果。 您將不得不異步使用該函數,就像您在代碼中具有異步操作的其他地方所做的那樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.