Combine redis keys into a response

Viewed 401

I have a Redis server with 2 keys. I'm trying to fetch both the keys and combine them into 1 response. I'm using node-redis.

My code:

var res = [];
RedisClient.keys('*', async function (err, keys) {
  if (err) return console.log(err);
  if (keys) {
    keys.forEach((key) => {
      RedisClient.get(key, (err, data) => {
        if (err) throw err;

        res.push(data);
      });
    });
    return res.status(200).send(JSON.parse(res));
  }
});

When I run this I get: UnhandledPromiseRejectionWarning: SyntaxError: Unexpected end of JSON input [0] at JSON.parse (<anonymous>). If I remove JSON.parse I get a response but it's an empty array.

I created my answer off of this solution

0 Answers
Related