从 NodeJS 将数组插入到 redis

Fog*_*ert 2 redis node.js node-redis

我有一个数组,我填写了一个这样的:

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

之后我验证数组是否包含所需的值(它包含):

console.log("Array after the for: \n");
console.log(obj);
Run Code Online (Sandbox Code Playgroud)

我试图挽救这下Redis的关键,但replyundefined

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

我也试过rpush,但没有运气。
可能是什么问题呢?

小智 9

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);
});
Run Code Online (Sandbox Code Playgroud)

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