Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return hgetall list from redis in nodejs

I'm trying to return a json object so that I can pass it back before a page is rendered to populate a list. My problem is that I can't figure out how to pass the object data out from the hgetall callback function. Here is my example with comments on what I'm missing:

var redis = require("redis"),
    client = redis.createClient();

function createMobs() {

    var mobObject = {
        name: "Globlin",
        hp: 12,
        level: 1
    };
    client.hmset("monsterlist", "mobs", JSON.stringify(mobObject));

    var myMobs = function(object) {
        return object;
    };

    var getMobs = function(callback) {   
      client.hgetall("monsterlist", function(err, object) {
        callback(object);
      });    
    };

    // This is returning undefined instead of my mob
    console.log("mobs: ", getMobs(myMobs));

    // Goal is to return moblist
    // return getMobs(myMobs);

}

exports.createMobs = createMobs;
like image 878
Organiccat Avatar asked Mar 11 '26 05:03

Organiccat


2 Answers

The short answer is that you're not thinking asynchronously. Because you're using asynchronous functions in your function, your function must also be asynchronous.

Since you didn't post the rest of your code, here's the basic idea:

var client = require('redis').createClient();

function createMobs(callback) {
    var mobObject = { name: 'Goblin' };

    client.hmset('monsterlist', 'mobs', JSON.stringify(mobObject), function(err) {
        // Now that we're in here, assuming no error, the set has went through.

        client.hgetall('monsterlist', function(err, object) {
            // We've got our object!

            callback(object);
        });

        // There is no way to run code right here and have it access the object variable, as it would run right away, and redis hasn't had time to send you the data yet. Your myMobs function wouldn't work either, because it is returning a totally different function.
    });
};

app.get('/create', function(req, res) {
    createMobs(function(object) {
        res.render('mobs.jade', {
            mobs: object
        });
    });
});

Hopefully that helps clear things up.

like image 94
ZachRabbit Avatar answered Mar 13 '26 19:03

ZachRabbit


The only way to deal with hgetall returning value are by Promises.

get all function:

 async hashget(tag) {
        return new Promise((resolve, reject) => {
          redis.createClient({ port: portnumber, host: config.redis.host 
           }).hgetall(tag, (err, object) => {
            if (err) {
              reject(err);
            } else {
              resolve(Object.keys(object));
            }
          });
        });
      }

and handle it in the through the promise as

const result = await this.redis.hashget('asize').then((result) => {
        return result;
 });
return result;
like image 32
sb39 Avatar answered Mar 13 '26 19:03

sb39



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!