node - sequelize多参数查询写法

查看:480
本文介绍了node - 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;
        }
    }

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

解决方案

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

这篇关于node - sequelize多参数查询写法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆