【问题标题】:Sequelize 'where' only if req.query is defined仅在定义了 req.query 时才续集“where”
【发布时间】:2014-09-29 23:35:25
【问题描述】:

我想仅在定义了“req.query.p”时才考虑 where 子句:

exports.select = function(req, res) {
        db.Pelicula
            .findAndCountAll({
                limit : req.query.limit,
                offset : req.query.offset,
                where : ["nombre = ?", req.query.p]
            })
            .success(function(resp){
                console.log(JSON.stringify(resp.rows, null, 4).bold.get);
                res.json({peliculas : resp.rows, meta : { total : resp.count}});
            });
}

【问题讨论】:

    标签: node.js express sequelize.js


    【解决方案1】:

    你可以这样做:

    where : req.query.p ? ["nombre = ?", req.query.p] : null
    

    【讨论】:

    • 如果 req.query.p 是 'val1 : 1' 我想按 val1 列过滤
    猜你喜欢
    • 1970-01-01
    • 2013-06-23
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 2018-07-18
    • 1970-01-01
    • 2022-11-11
    相关资源
    最近更新 更多