Redis
 sql >> Datenbank >  >> NoSQL >> Redis

Gibt die hgetall-Liste von redis in nodejs zurück

Die kurze Antwort ist, dass Sie nicht asynchron denken. Da Sie in Ihrer Funktion asynchrone Funktionen verwenden, muss Ihre Funktion ebenfalls asynchron sein.

Da Sie den Rest Ihres Codes nicht gepostet haben, hier die Grundidee:

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

Hoffentlich hilft das bei der Klärung.