Sunday, 28 May 2017

How to pass all redis records to view?

I want to pass all records from Redis to a view. I think i'm doing something wrong with the redis values be cause i can't push it to array or object.

I've tried to to like so :

app.get('/', function (req, res, next) {
var items = [];
  client.keys('*', function (err, obj) {

    for (var i = 0, len = obj.length; i < len; i++) {

        client.hgetall(obj[i], function (err, value) {
            if(typeof value === 'object'){
                items.push(value);
            }

        });

    }
    console.log(items); // returns empty array

});

   res.render('searchusers'); // need to pass the object here
});

When i console log the Value i get

  for (var i = 0, len = obj.length; i < len; i++) {

        client.hgetall(obj[i], function (err, value) {
            console.log(value);

        });

    }
 ------------ Result--------------

{ first_name: 'john123',
  last_name: 'foofoo',
  email: '323233',
  phone: 'foo' }

Value is obviously an object...do i need to do a for loop over it's values? or perhaps there is a more simple way to do so.



via RoyBarOn

No comments:

Post a Comment