在节点js中的foreach内从redis插入数据到数组中

时间:2016-10-04 07:06:12

标签: node.js foreach node-redis

我有一个节点js redis api我在哪里读取基于密钥的redis数据,如果不存在则从另一个api获取数据然后将这些数据插入redis。此外,我将此数据推送到变量中,在foreach的末尾,我想将此值返回给最终用户。

问题是redis在异步中运行,因此我的数据变量总是为空。我该如何解决这个问题?

以下是我的代码的相关部分

text_number.Attributes.Add("type", "number");
text_date.Attributes.Add("type", "date");
text_time.Attributes.Add("type", "time");

});

输出总是像 between.forEach(function(entry) { asyncTasks.push(function(callback){ client.exists(id+entry, function(err, reply) { if (reply === 1) { client.get(id+entry, function(err, reply) { var output = JSON.parse(reply); data_output = data_output.concat(output); }); } else { process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; var slot_url = "https://" + username + ":" + password + "@testapi.foobar.com/1/timeslots?productId=" + id + "&fromDate=" + entry + "&toDate=" + entry; request({ url: slot_url, json: true, headers: headers }, function(error, response, body) { if (!error && response.statusCode === 200) { var data = []; try { var temp = {}; console.log(body); body.data.forEach(function(tempslots) { temp['date'] = tempslots['date']; temp['timeslots'] = tempslots['timeslots']; data = data.concat(temp); }); client.set(id+entry, JSON.stringify(data)); data_output = data_output.concat(data); } catch (err) { console.log(err.message); } } else { console.log("Something went wrong!! " + error.message); } }) } callback(); }); }); }); async.parallel(asyncTasks, function(){ // All tasks are done now result['data'] = data_output; result['response'] = 1; result['message'] = 'vacancies list fetched successfully!'; res.json(result); }); 一样,即使我已经检查过代码是否正在提取并在redis中正确插入数据

1 个答案:

答案 0 :(得分:0)

var totalLengthOfItem = between.length;
for (var i = 0; i < totalLengthOfItem - 1; i++) {
    var entry = between[i];
    client.exists(id + entry, function (err, reply) {
        if (reply === 1) {
            client.get(id + entry, function (err, reply) {
                var output = JSON.parse(reply);
                data_output = data_output.concat(output);
                if (i == totalLengthOfItem - 1) {
                    response(data_output);
                }
            });
        } else {
            process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
            var slot_url = "https://" + username + ":" + password + "@testapi.foobar.com/1/timeslots?productId=" + id + "&fromDate=" + entry + "&toDate=" + entry;
            request({

                url: slot_url,
                json: true,
                headers: headers
            }, function (error, response, body) {
                if (!error && response.statusCode === 200) {
                    var data = [];
                    try {
                        var temp = {};
                        body.data.forEach(function (tempslots) {
                            temp['date'] = tempslots['date'];
                            temp['timeslots'] = tempslots['timeslots'];
                            data = data.concat(temp);
                        });
                        client.set(id + entry, JSON.stringify(data));
                        data_output = data_output.concat(data);
                        if (i == totalLengthOfItem - 1) {
                            response(data_output);
                        }
                    } catch (err) {
                        console.log(err.message);
                    }
                } else {
                    console.log("Something went wrong!! " + error.message);
                }
            })
        }
    });

}

function response(data_output) {
    result['data'] = data_output;
    result['response'] = 1;
    result['message'] = 'Capacity list fetched successfully!';
    res.json(result);
}
})