0

Here's the code:

var redis = require('redis');
var client = redis.createClient();
var addresses = new Array();

client.lrange('some_list', 0, -1, function(err, res) {
  console.log(res); #inner

  res.forEach(function(element) {
    if (result = element.match(/^(.+),(.*)$/)) {
      addresses.push(result[1]);
    }
  });
});

console.log(addresses); #outter

results in

[] #outter
['item1', 'item2', 'item3'] #inner

It turns out that the outside console log execute first then return null array. I want to get array result from node-redis lrange method.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Feuda
  • 2,335
  • 30
  • 28

2 Answers2

0

In case you want to see what addresses holds then you can try something like this:

const redis = require('redis');
const client = redis.createClient();
let addresses = new Array();
client.lrange('some_list', 0, -1, (err, res) => {
  console.log(res);
  res.forEach((element) => {
    if (result = element.match(/^(.+),(.*)$/)) {
      addresses.push(result[1]);
    }
  });
  console.log(addresses);
});
Neeraj Wadhwa
  • 645
  • 2
  • 7
  • 18
  • And you can use `let addresses = [];` instead of `let addresses = new Array();`, you can read more [here](https://stackoverflow.com/questions/931872/what-s-the-difference-between-array-and-while-declaring-a-javascript-ar) – Neeraj Wadhwa May 21 '18 at 12:06
  • Thanks for your response. I want to get array result from node-redis lrange method and save it to a variable then use it in another function. – Feuda May 21 '18 at 12:07
  • You are already saving the response from `lrange()` in `addresses` but the thing is that Node.js is asynchronous, that means that `client.lrange()` method will take some time to complete but Node.js won't stop from executing rest of the script and that's why `console.log(addresses);` gives an empty array. – Neeraj Wadhwa May 21 '18 at 12:11
  • So how to get the full array from outside? – Feuda May 21 '18 at 12:13
  • 1
    you can do something like this: `const redis = require('redis'); const client = redis.createClient(); const someFunc = () => { return new Promise((resolve, reject) => { client.lrange('some_list', 0, -1, (err, res) => { if (err) { reject(err); } else { let addresses = []; res.forEach((element) => { if (result = element.match(/^(.+),(.*)$/)) { addresses.push(result[1]); } }); resolve(addresses); } }); }); }` – Neeraj Wadhwa May 21 '18 at 12:21
  • I want to use the addresses outside the lrange method but not in, that's why I console log the addresses at the outside end. – Feuda May 22 '18 at 03:34
  • After revised your code, things get to work, thank you. – Feuda May 22 '18 at 04:24
  • 1
    You can use `.then()` or `await` it inside other `async` function to get the value. – Neeraj Wadhwa May 22 '18 at 04:28
0
const driver = await redis.lRange("allDriverList", 0, -1);
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Sep 27 '22 at 15:52