简体   繁体   English

如何在 NodeJs 中找到条件?

[英]How to find with condition in NodeJs?

i have a controller that i want to search or find with condition.我有一个 controller 我想搜索或找到条件。 Can anybody help me or give me some suggestions so that i can find data with more conditions.任何人都可以帮助我或给我一些建议,以便我可以找到更多条件的数据。 Let me demonstrate my code below:让我在下面演示我的代码:

// 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."
        });
      });
}

How can i set the condition more with COLOR and BRAND.如何使用颜色和品牌更多地设置条件。 I am very appreciate for every help我非常感谢每一个帮助

Try adding a comma:尝试添加逗号:

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