gpt4 book ai didi

javascript - 使用 NODE JS 和 BLUEBIRD 从 REDIS 中获取数据

转载 作者:可可西里 更新时间:2023-11-01 11:20:40 26 4
gpt4 key购买 nike

我正在尝试获取特定键模式的所有条目并使回调整齐地发生,我正在使用 Bluebird。项目的nodejs的redis客户端是node_redis。

redis客户端中的代码是-

    exports.getAllRedisKeysA = function() {
var res = rclient.keysAsync("client*").then(function(data) {
// console.log(data);
}).then(function(data) {
var arrayResp = [];
for (var element in data) {
rclient.hgetallAsync(element).then(function(data) {
arrayResp.push(data);
});
};
return arrayResp;
// console.log(data);
}).catch(console.log.bind(console));
console.log(res); // gives an empty promise.
return res;
}

并且这个函数是通过以下方式从 Controller 中调用的 -

var abc = rdata.getAllRedisKeysA();
// console.log(abc); // gives undefined

redis 函数内的 console.log 输出给出了一个空的 promise ,没有任何内容返回给 Controller 。

我在实现过程中遗漏了什么吗?

最佳答案

LinusJaromanda对这个问题有一些真正有用的评论,帮助我朝着正确的方向前进。我已经使用以下方法使用 BlueBird Promise 从 REDIS 中获取我需要的数据,这是需要完成的方法。

下面的代码从REDIS中获取所需的数据

exports.getRedisKeyPattern = function(pattern){

var allKeys = allKeysPattern(pattern).then(function(data){
var arrayResp = [];
var inptArr = [];
var newdata = data.slice(1)[0];
for(var i in newdata){
inptArr.push(newdata[i]);
};
return new Promise.resolve(inptArr);
});

var valuePerKey = Promise.mapSeries(allKeys, function(dt){
return getAllKeyContents(dt);
}).then(function(res){
return res;
}).catch(function(err) { console.log("Argh, broken: " + err.message);
});

return new Promise.resolve(valuePerKey);
}

function getAllKeyContents(key){
var data = rclient.hgetallAsync(key).then(function(data){
return data;
}).catch(function(err) { console.log("Argh, broken: " + err.message); });

var res = Promise.join(data, key, function(data, key){
return {
key: key,
data: data
};
});

return res;
}

在 Controller 中,函数是这样调用的——

var rdata = require('../../components/redis/redis-functions');
rdata.getRedisKeyPattern("clients*").then(function(response){
return res.status(200).json(response);
});

包含redis函数的.js文件包含在 Controller 文件中,以便可以使用函数。

关于javascript - 使用 NODE JS 和 BLUEBIRD 从 REDIS 中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34944568/

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