简体   繁体   中英

Array.push is not retaining the array value in node.js

I have promisified the code of caching dataand pushing the array of Json values in the new array but after the chaining, array is showing undefined. Here is the code snippet.

'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)});
});

Conclusion -> message is undefined

message is not in scope. A quick fix would be to declare message outside of your promise chain (directly under const results = ... ).

There are also some other funky things going on with this code - map isn't really mapping anything and some of your then s don't return a value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM