17. 接口6-模糊查询

2024-10-28  本文已影响0人  求墨者

// admin/articles.js

router.get('/', async function (req, res, next) {
  try {
    const condition = {
      order: [['id', "DESC"]]
    }

    const { title } = req.query

    if (title) {
      condition["where"] = {
        title: {
          [Op.like]: `%${title}%`
        }
      }
    }

    const articles = await Article.findAll(condition);

    res.json({
      status: true,
      message: "查询列表成功。",
      data: {
        articles
      }
    });

  } catch (error) {
    res.status(500).json({
      error
    })
  }
});

上一篇 下一篇

猜你喜欢

热点阅读