gpt4 book ai didi

javascript - Node js redis循环遍历每个哈希键值

转载 作者:IT王子 更新时间:2023-10-29 06:07:50 26 4
gpt4 key购买 nike

我是 redis 和 Node 的新手,目前我正在尝试遍历我创建的一些测试哈希键并打印出来以筛选结果。这是我希望看到的结果:

{ "aaData": [['Tim Osbourne'],['Joe Bloggs'],['John Doe'],['Perry Coke'],['Will Holmes'],['Steven Smith']}

但是我得到了这个结果:

{ "aaData": [[],[],[],[],[],[],]}'Tim Osbourne','Joe Bloggs','John Doe','Perry Coke','Will Holmes','Steven Smith',

这是我的代码:

    app = require('../app');    var redis = require("redis"),    client = redis.createClient();    routes = require('./');    var key_types = '';    client.keys("*", function (err, all_keys) {        key_types += '{ "aaData": [';        all_keys.forEach(function (key, pos) { // use second arg of forEach to get pos                  key_types += "[";            client.hmget([key, 'Owner of space'], function(err, field_val){                key_types = key_types + "'" + field_val + "',";            });            key_types += "],";        });        key_types += "]}";                   });    app.get('/table_data', function(req, res){        res.render('table_data', { keys: key_types});    });

最佳答案

  • 你不应该做 keys *
  • 它不起作用,因为 hmget 是异步的,您应该为此使用 async 模块 (async.map)。

[key, 'Owner of space'] 的目标是什么,因为 'Owner of space' 总是会产生相同的结果?

关于javascript - Node js redis循环遍历每个哈希键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23247736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com