aya
aya

Reputation: 1613

how store php array to redis and retrieve in nodejs?

I use redis (predis/predis composer) to save some id in php like this:

$redis = new Client();
$mailJson = $redis->get('mail');
$mail  = json_decode($mailJson);
$no = rand(100,500);
array_push($mail, $no);
$redis->set('mail', json_encode($mail));

and i retrieve this array like this:

var redis = require("redis"),
    client = redis.createClient();
client.on('connect', function() {
    client.get('mail', function(err, mailIds) {
        console.log(mailIds);
    });
});

But mailIds variable is string and not an array like this:

[1,200,500,500,400,100,200,100]

Is way can access mailIds items ?

Upvotes: 1

Views: 161

Answers (1)

jeroen
jeroen

Reputation: 91734

You need to parse the json (a string) so that you get an array in javascript / node.js:

client.get('mail', function(err, mailIds) {
    // parse the json
    mailIdsArray = JSON.parse(mailIds);
    console.log(mailIdsArray);
});

Upvotes: 3

Related Questions