在第一场比赛中停止Redis hget
Stop a Redis hget on first match
我正在用Node.js、Express&Redis和我需要你的帮助。
app.get('/match', function(req, res) {
db.hkeys('testCollection', function(err, keys){
console.log(keys.length + " keys in Redis:");
keys.forEach(function (keyPlayer, i){
db.hget('testCollection', keyPlayer, function(err, obj){
var testObj = JSON.parse(obj);
if ([conditions]){
console.log('--- A match has been found! ---');
console.log('--- Details of match ---');
console.log('ID: ' + keyPlayer);
console.log('Name: ' + testObj.name);
res.render('match', {
match : testObj
});
}
else {
console.log('*** No match has been found ***');
if ((i+1) == keys.length){
console.log('=== No player match found in DB ===');
};
}
});
});
});
});
当[if]中的条件被满足时,我如何使这行代码停止?这可能吗?或者我应该寻找不同的解决方案吗?
db.hget('testCollection', keyPlayer, function(err, obj){
以下是使用async
:的解决方案
var async = require('async');
// ...
app.get('/match', function(req, res) {
db.hkeys('testCollection', function(err, keys){
console.log(keys.length + " keys in Redis:");
async.eachSeries(keys, function(keyPlayer, cb) {
db.hget('testCollection', keyPlayer, function(err, obj) {
if (err) return cb(err);
var testObj = JSON.parse(obj);
if ([conditions]){
console.log('--- A match has been found! ---');
console.log('--- Details of match ---');
console.log('ID: ' + keyPlayer);
console.log('Name: ' + testObj.name);
res.render('match', {
match : testObj
});
return cb(true);
}
console.log('*** No match has been found ***');
cb();
});
}, function(err) {
if (!err)
console.log('=== No player match found in DB ===');
if (err && err !== true)
console.log('Error: ' + err);
});
});
});
相关文章:
- '会话'未定义-会话存储的Este JS/Express/Redis
- 来自redis和nodejs的多个值
- node.js全局数组与redis
- redis服务器脱机使节点崩溃
- 用Redis扩展Socket.IO,然后扩展Redis本身
- 单个 Redis 客户端连接是否支持并发读写
- 如何在 node.js 中查询 redis 中的值
- 节点redis't喜欢函数.apply()
- 从redis到elasticsearch的Node.js脚本副本内存不足
- Node js redis socket.io pubsub实时更新
- 如何在数据库断开连接时处理Redis会话
- 对电子邮件(或用户名)进行哈希处理以存储在redis哈希桶中
- Connect Redis不保留数据
- 如何在 redis 中将 JS 对象传递给它自己的事件参数 - 连接事件上的哨兵
- Redis 客户端不会删除任何内容
- 服务器发送事件未正确(刷新?)使用Flask和Redis与nginx前端
- 如何将 javascript 数组另存为 redis 列表
- 转换 Redis 密钥时的内存管理
- 如何在 Redis 中执行持久性存储
- 在第一场比赛中停止Redis hget