rendering results of multiple DB/mongoose queries to a view in express.js

后端 未结 3 1229
无人及你
无人及你 2020-12-30 16:26

given the async nature of mongoose (or sequelize, or redis) queries, what do you do when you have multiple queries you need to make before rendering the view?

For in

3条回答
  •  星月不相逢
    2020-12-30 16:33

    Yep, this is a particularly annoying case in async code. What you can do is to put the code you'd have to duplicate into a local function to keep it DRY:

    exports.index = function (req, res) {
        var current_user = null
    
        Player.find({last_logged_in : today()}).exec(function(err, players) {
            if (err) return res.render('500');
    
            function render() {
                res.render('game/index', { title: 'Battle!',
                           players: players,
                           game_is_full: (players.length >= 6),
                           current_user: current_user
                });
            }
    
            if (req.session.user_id) {
                Player.findOne({_id : req.session.user_id}).exec(function(err, player) {
                    if (err) return;
                    if (player) {
                        current_user = player
                    }
                    render();
                })
            } else {
                render();
            }
        });
    };
    

    However, looking at what you're doing here, you'll probably need to look up the current player information in multiple request handlers, so in that case you're better off using middleware.

    Something like:

    exports.loadUser = function (req, res, next) {
        if (req.session.user_id) {
            Player.findOne({_id : req.session.user_id}).exec(function(err, player) {
                if (err) return;
                if (player) {
                    req.player = player
                }
                next();
            })
        } else {
            next();
        }
    }
    

    Then you'd configure your routes to call loadUser wherever you need req.player populated and the route handler can just pull the player details right from there.

提交回复
热议问题