簡體   English   中英

Node.js使用node_redis進行回調

[英]Node.js callback with node_redis

我仍然試圖進入node.js並且可能會得到一些不太正確的東西。 我正在尋找的目標是首先查詢包含房間列表的hmap。 這個列表將通過迭代來獲取每個房間的更多細節,如房間名稱等。

以下是查詢應返回的內容:

redis 127.0.0.1:6379> lrange rooms 0 -1
1) "room:5000"

redis 127.0.0.1:6379> hgetall room:5000
1) "name"
2) "room1"
3) "admin"
4) "user:1001"
5) "public"
6) "true"

這是我在routes.index中的功能

exports.index = function(req, res){

    var render_rooms = new Array();

    req.app.settings.redis.lrange('rooms',0,-1, function(error, rooms) {

        if (error) {
            console.log('Error: '+ error);
            }
        else {

            rooms.forEach(function(room){
                console.log("room: " + room);

                req.app.settings.redis.hgetall(room, function(error, roomdetails){
                    if (error) {
                        console.log('Error: '+ error);
                        }
                    else {
                        console.log("roomdetails: " + roomdetails.public);

                        if(roomdetails.public == "true"){
                            render_rooms.push(roomdetails.name);

                        }

                    }
                });

            });




        //  console.log('Name: ' + result);
      //    res.render('account', { title: 'account title', user: req.user.username, votes: result });
        }

    });


    console.log("length: " + render_rooms.length);
    res.render('index', { title: 'Index', username: req.user.username, rooms: render_rooms });

};

我不確定我是否正確使用node_redis來實現這一點。 此外,我想出了將所有房間細節存儲在一個陣列中的想法,我希望將其發送到視圖中。 顯然列表總是沒有顯示任何元素,因為我猜是在列表填充之前調用,因為我缺少一些基本的回調功能。 無論如何我都無法擺弄它。有人可以更詳細地解釋一下它應該如何運作嗎?

您的基本問題是需要等待渲染render_rooms數組,直到所有異步處理完成。 現在編寫它的方式是在任何異步Redis查詢完成之前調用res.render

像這樣的東西:

exports.index = function(req, res){

    var render_rooms = new Array();

    req.app.settings.redis.lrange('rooms',0,-1, function(error, rooms) {

        // Keep track of the number of rooms we have left to process.
        var roomcount = rooms.length;

        if (error) {
            console.log('Error: '+ error);
            }
        else {

            rooms.forEach(function(room){
                console.log("room: " + room);

                req.app.settings.redis.hgetall(room, function(error, roomdetails){
                    if (error) {
                        console.log('Error: '+ error);
                        }
                    else {
                        console.log("roomdetails: " + roomdetails.public);

                        if(roomdetails.public == "true"){
                            render_rooms.push(roomdetails.name);
                        }

                        // Render code moves to here and is only run after all rooms
                        // have been processed.
                        if (--roomcount === 0) {
                            console.log("length: " + render_rooms.length);
                            res.render('index', { 
                                title: 'Index', 
                                username: req.user.username, 
                                rooms: render_rooms 
                            });
                        }
                    }
                });
            });
        }
    });
};

一旦你對這方面的工作感到滿意,可以使用async.forEachasync.forEachSeries稍微清理它,它可以更干凈地支持這種類型的流程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM