Chain filter conditions dynamically

Viewed 717

How to chain multiple conditions in RethinkDB? This is what I got right now and what works if I only pass live or sports as a parameter. As soon as I pass the live and sports parameter, sports obviously always overwrites the filter variable and the live parameter is ignored.

app.get('/bets', function (req, res) {
    var live = req.query.live;
    var sports = req.query.sports;

    var filter = {};

    if (live === undefined) {
        filter = r.or(r.row('live').eq(0), r.row('live').eq(1));
    } else {
        filter.live = parseInt(live);
    }

    if (sports !== undefined) {
        var sports = sports.split(',');
        filter = function (doc) {
            return r.expr(sports).contains(doc("sport"));
        }
    }

    r.table('bets').filter(filter).limit(100).run(connection, function(err, cursor) {
        // ...
    });
});
1 Answers
Related