gpt4 book ai didi

express - Sequelize - 通过匹配所有标签来过滤 FindAll

转载 作者:行者123 更新时间:2023-12-03 22:21:09 25 4
gpt4 key购买 nike

/assets/?tags[]=foo&tags[]=bar
对上述端点的获取请求应仅返回包含所有提供的标签( foobar )的记录
我目前的尝试是返回与任何给定标签匹配的任何记录。
const { tags } = req.query;

res.send(
await Asset.findAll({
where: whereOptions,
include: [
{ model: AssetMime },
{
model: Tag,
as: 'tags',
where: {
title: {
[Op.in]: tags,
},
},
},
],
})
);
如何修改我的过滤器以返回所有标签匹配的记录?

最佳答案

如果您的 DBMS 支持 Op.all 运算符,您可以尝试 op.in 运算符而不是 ALL
另一种解决方案是结合这些条件

const where = {}
if (tags && tags.length) {
where[Op.and] = tags.map(x => ({ title: x })
}

await Asset.findAll({
where: whereOptions,
include: [
{ model: AssetMime },
{
model: Tag,
as: 'tags',
where: where,
},
],
})

关于express - Sequelize - 通过匹配所有标签来过滤 FindAll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63631187/

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