sequelize多参数查询写法

我查询内容的时候

return class UserController extends app.Controller {
        * index(ctx) {
            if(ctx.query.page){
                var page=ctx.query.page;
            }else{
                var page=1;
            }
            var pageLength=12;
            var offesrNum=(page-1)*pageLength;
            console.log(ctx.query.page);
            const articles = yield this.ctx.model.Article.findAndCountAll({
                limit:pageLength,offset:offesrNum
            });
            this.ctx.body=articles;
        }
    }

但是我有时是要查询某栏目的articles
这是时候我是这样写的

return class UserController extends app.Controller {
        * index(ctx) {
            if(ctx.query.page){
                var page=ctx.query.page;
            }else{
                var page=1;
            }
            var pageLength=12;
            var offesrNum=(page-1)*pageLength;
            console.log(ctx.query.page);
            if(ctx.query.menuId){
             const articles = yield this.ctx.model.Article.findAndCountAll({
                limit:pageLength,offset:offesrNum,where:{menuId:ctx.query.menuId}
             });
            }else{
             const articles = yield this.ctx.model.Article.findAndCountAll({
                limit:pageLength,offset:offesrNum
             });
            }
            this.ctx.body=articles;
        }
    }

感觉这样写好麻烦,如果还有其他帅选条件的话又要写很多判断然后写很多。
有没有什么办法合并成一个通用查询?
请教下大家。

阅读 3.2k
1 个回答

自己吧参数包装下,自己放再where语句里

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题