Array.push没有在node.js中保留数组值

时间:2017-06-11 21:55:51

标签: javascript redis

我已经宣传缓存数据的代码并在新数组中推送Json值数组但是在链接之后,数组显示未定义。这是代码段。

'use strict';
const Promise = require('bluebird');
let _connectResolve, _connectReject, onConnected = new Promise((resolve, reject) => {
        _connectResolve = resolve;
        _connectReject = reject;
    }),
    redis = require("redis"),
    redisClient = redis.createClient({
        host: 'localhost',
        port: 6379
    });
Promise.promisifyAll(redis.RedisClient.prototype);

redisClient.on('connect', _connectResolve);
const results = Promise.all([
    'iems/0I0g2I92u0U3k/120s.zip',
    'ims/25213u0X462g/10es.zip',
    'ims/2x0n440V1A0f1K/F.zip',
    'its/2l0239311f1u0w1S2a3j/Files.zip',
    'its/2O2x212i0a2f1j3t0B2h/Files.zip',
]);
onConnected.then(() => {
      var message = [];
      results.map(function(result) {
          redisClient.getAsync(result).then((reply) => {
              return new Promise((resolve, reject) => {
                  resolve({
                      'response': reply,
                      'req': result
                  });
              });
          }).then(function(reply) {
              if (reply['response'] != true) {
                  message.push({
                      "key": reply['req'],
                      "bucket_name": 'string here'
                  });
              }
          });
    }).then(function(){console.log(message)});
});

结论 - >消息未定义

1 个答案:

答案 0 :(得分:1)

message不在范围内。快速解决方法是在您的保证链之外(直接在const results = ...下)声明消息。

此代码还有一些其他时髦的事情 - map并未真正映射任何内容,而您的某些then不会返回值。