7

I have an array which I fill up in a for like this:

var obj = [];
for(i = 0; i < data.bids.length; i += 1) {
 obj.push(JSON.parse(data.bids[i][0]));
}

After that I verify if the array contains the desired values (it contains):

console.log("Array after the for: \n");
console.log(obj);

I try to save this under a redis key, but the reply is undefined

client.set('order-book:buy:bitstamp', obj, function(err, reply) {
 console.log(reply);
});

I've also tried with rpush, but no luck.
What could be the problem?

asked May 8, 2018 at 5:33
3
  • 1
    Did you check err? .set expects a string, it looks like you want client.set(key, JSON.stringify(obj), function (err, reply) { if (err) { console.error(err); } console.log(reply); }); Commented May 8, 2018 at 5:37
  • client.rpush(key, array, callback) is likely what you'll end up with since list is the more natural data type for your data, it'll fail if the key is already set to some other data type (string if you used a set) Commented May 8, 2018 at 5:49
  • It worked with JSON.stringify, if I use rpush I get undefined. Commented May 8, 2018 at 6:10

1 Answer 1

16

You can check here which types are compatible as redis values data-types-intro.

If you need to save array of objects in redis, the only way to do this is by using JSON.stringify(obj). So in your example it would look something like:

const redisValue = JSON.stringify(obj);
client.set('order-book:buy:bitstamp', redisValue, function(err, reply) {
 console.log(reply);
});

When reading data from redis you can easily use JSON.parse(redisResponse).

answered May 9, 2018 at 7:43
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.