鍍金池/ 問(wèn)答/數(shù)據(jù)庫(kù)  HTML/ sequelize字段過(guò)濾

sequelize字段過(guò)濾

需求:

  • url傳type,sequelize則查詢select * from xxx where type = xxx
  • url不傳type,sequelize則查詢select * from xxx

例:

  • localhost:3000/admin/metas/list
對(duì)應(yīng)sql:select * from metas
  • localhost:3000/admin/metas/list?type=1
對(duì)應(yīng)sql:select * from metas where type = 1

clipboard.png

這樣寫(xiě)不行,不傳type時(shí)候查的是 select * from metas where type = null

第一次用sequelize,請(qǐng)大神指點(diǎn)

回答
編輯回答
壞脾滊

不傳type的時(shí)候請(qǐng)求是不是這樣的 localhost:3000/admin/metas/list?type=

2017年2月10日 14:01
編輯回答
乖乖瀦

把查詢條件拿出來(lái)就行了:

let query = {};
if (ctx.request.query.type) {
    query.type = ctx.request.query.type;
}
await Metas.findAll({
    where: query
})
2017年1月31日 09:01