首先模糊查询要用到Op,一定要导入,否则会报错
// 使用模糊查询需要先引入Op
const seq = require('sequelize');
const Op = seq.Op;
然后是查数量,本人习惯是先查数量,不为0再去查详细数据:
这里的 user_name就是本人要检索的字段, ${user_name} 中的user_name是前端传的:
let count = await User.count({
where: {
user_name: {
[Op.like]: `%${user_name}%`
},
}
});
查详细列表同理,使用 attributes 过滤掉密码字段,使用 limit和offset来分页,pageSize和pageIndex是前端传的分页数据;
let res = await User.findAll({
attributes: {exclude: ['password']},
where: {
user_name: {
[Op.like]: `%${user_name}%`
},
},
limit: pageSiz