gpt4 book ai didi

postgresql - 如何在 Sequelize.js 的 where 子句中使用 if 条件

转载 作者:行者123 更新时间:2023-11-29 14:14:54 24 4
gpt4 key购买 nike

仅当 manufacturer_id 不等于 null 时,我才想查询 ManufacturerId: manufacturer_id

我试过这段代码,但它给我语法错误:

/* Get Brands List */
router.get('/', function(req, res) {
var archived_status = req.query["archived-status"];
var manufacturer_id = req.params.manufacturer_id;
models.Brand.findAll({
where: {
brand_archived_status: archived_status,
if(manufacturer_id != null){
ManufacturerId: manufacturer_id
}
},
include: [{
model: models.Job,
required: false
}]
}).
then(function(brands) {
res.status(200).json(brands);
}, function(error) {
res.status(500).send(error);
});
});

最佳答案

你为什么不在 Sequelize 查询之外创建整个 where 条件,给你:

let where = {
brand_archived_status: archived_status
};
if (manufacturer_id != null) {
where['ManufacturerId'] = manufacturer_id
}

models.Brand.findAll({
where,
include: [{
model: models.Job,
required: false
}]
})

这看起来干净简单:)

关于postgresql - 如何在 Sequelize.js 的 where 子句中使用 if 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52442389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com