0

When I use rpop on a list that I previously had pushed an array containing 4 values to, Redis returns all 4 values as expected. My problem is that they are returned as single values. Is there a way to have Redis return an array containing the 4 values as opposed to returning the 4 values the array contains? I am using the node_redis client https://github.com/NodeRedis/node_redis. Here is some code to give you an idea of what I am doing.

redisClient.lpush(data.pushKey, data.rowTest);

data.rowtest looks like this [ 0, 496, 1, 48 ].

 redisClient.rpop(data.popKey, function (err, res) {
    if (err) { console.log(err); }

    if (res !== null) {
    client.send("fromList", { content: res });
    console.log(res);
    }
    });

I am expecting an output of [ 0, 496, 1, 48 ] on a single line, but I am getting 0 496 1 48 each on a separate line.

Ok, I'm pretty sure I need to learn how to work with asynchronous javascript, before I solve my issue above.

Pfrex
  • 159
  • 2
  • 13

2 Answers2

0

You need to decide if it's a single element ot 4 elements.

If you want to treat each value as a single element, you need to send them separately, see this answer, then you will need to ask for all the 4 when you want to pop it back.

If you want the whole array as a single value, you can stay with your code and transform the result back to array with res.split('\n')

yeya
  • 1,968
  • 1
  • 21
  • 31
  • I tried playing around with .split and it was confusing. I think I may be confused about the async nature of node_redis. Is it returning each rpop asynchronously and that is why it is on 4 separate console output lines? Because, res.split(' ') returned 4 separate arrays and not an array containing 4 values. If that is the case I would be forced to use lrange and lrem. – Pfrex Dec 17 '18 at 23:18
0

Ok, this should have been more obvious and it leaves me with some other questions, but I solved my issue by using JSON.stringfy() before pushing and JSON.parse() after popping. I'm going to guess that the node_redis developers wanted to maintain the similarity between arrays and Redis lists, and so took the array values into the Redis list... that or Redis lists are simply arrays and as such arrays can just be concatenated to an existing list.

Pfrex
  • 159
  • 2
  • 13
  • That an option too, then each element is a single string that represent JSON object, it can be array or object what ever you want. – yeya Dec 18 '18 at 06:38