sql >> データベース >  >> NoSQL >> Redis

nodejsのredisからhgetallリストを返す

    簡単な答えは、非同期的に考えているのではないということです。関数で非同期関数を使用しているため、関数も非同期である必要があります。

    残りのコードを投稿しなかったので、基本的な考え方は次のとおりです。

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

    うまくいけば、それは物事を片付けるのに役立ちます。



    1. MongoDBの既存のフィールドの最後に文字列を追加します

    2. 在庫管理システムのSQLとNoSQL

    3. MongoDBの起動に失敗しました-***fassert()の失敗後に中止します

    4. spring-data-redisredisTemplate例外