我查询内容的时候
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语句里