sql >> Base de Datos >  >> NoSQL >> Redis

Devuelve la lista hgetall de redis en nodejs

La respuesta corta es que no estás pensando de forma asíncrona. Debido a que está utilizando funciones asíncronas en su función, su función también debe ser asíncrona.

Como no publicaste el resto de tu código, esta es la idea básica:

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
        });
    });
});

Esperemos que eso ayude a aclarar las cosas.