簡體   English   中英

如何在 NodeJs 中找到條件?

[英]How to find with condition in NodeJs?

我有一個 controller 我想搜索或找到條件。 任何人都可以幫助我或給我一些建議,以便我可以找到更多條件的數據。 讓我在下面演示我的代碼:

// Find all car with condition
export function findAllCar( req, res){
    const name = req.query.name;
    const color = req.query.color;
    const brand = req.query.brand;
    var condition = name ? { name: { [Op.iLike]: `%${name}%` } } : null;// Here i only can find with name, now i want to add more condition are color and brand

    Car.findAll({ where: condition })
      .then(data => {
        res.send(data);
      })
      .catch(err => {
        res.status(500).send({
          message:
            err.message || "Some error occurred while retrieving CARS."
        });
      });
}

如何使用顏色和品牌更多地設置條件。 我非常感謝每一個幫助

嘗試添加逗號:

var condition = name && color && brand ? { 
    name: { [Op.iLike]: `%${name}%` },
    color: { [Op.iLike]: `%${color}%` },
    brand: { [Op.iLike]: `%${brand}%` },
} : null;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM