如何发送阵列标记Id';在Strapi中使用AND逻辑,而不是它们之间的逻辑?[MongoDB]
我有一个ID数组,它填充在如何发送阵列标记Id';在Strapi中使用AND逻辑,而不是它们之间的逻辑?[MongoDB],mongodb,api,strapi,Mongodb,Api,Strapi,我有一个ID数组,它填充在数组中 我正在尝试使用过滤器向Strapi发送API get请求,如下所示: let listTagId = []; tagOptions.map((tag) => { if (inputValues[`${tag.nameTag}`]) { listTagId.push(tag.id); } }); let filter = { ...cleanObject({ 'tags.id': list
数组中
我正在尝试使用过滤器向Strapi发送API get请求,如下所示:
let listTagId = [];
tagOptions.map((tag) => {
if (inputValues[`${tag.nameTag}`]) {
listTagId.push(tag.id);
}
});
let filter = {
...cleanObject({
'tags.id': listTagId.length > 0 ? listTagId : null,
}
其中cleanObject
等于
function cleanObject(obj) {
let propNames = Object.getOwnPropertyNames(obj);
for (let i = 0; i < propNames.length; i++) {
var propName = propNames[i];
if (obj[propName] === null || obj[propName] === undefined) {
delete obj[propName];
}
}
return obj;
}
标记是与API-OBJECT
相关的集合。我想要实现的是只过滤那些包含所有tags.id
的API对象(tag.id1和tag.id2…)
目前,如果我按照上面的逻辑(代码)使用它使用的或逻辑,这意味着如果一个对象有一个标签在listTagId
中,它将从服务器检索结果
例如:
发送listTagId=['1223','345'],当前它检索id为'1223'或'345'的所有对象。因此,该对象可能仅与1223标记有标记关系,并且仍将被检索。对我来说是不正确的行为
发送listTagId=['1223','345'],调用应仅检索具有id为'1223'和'345'的标记的对象
从控制台中提取:
'GET /api-end-point/count?_where%5Bcategory_contains%5D=houses&_where%5Btags.id%5D%5B0%5D=60420fc8c4bed44584638dec&_where%5Btags.id%5D%5B1%5D=60420febc4bed44584638ded&_where%5Btags.id%5D%5B2%5D=6042151fbea9601908f2c774&_where%5Btags.id%5D%5B3%5D=60421520bea9601908f2c775&_where%5Btags.id%5D%5B4%5D=60421521bea9601908f2c776&_where%5Btags.id%5D%5B5%5D=60421522bea9601908f2c777&_where%5Btags.id%5D%5B6%5D=60421522bea9601908f2c778 (562 ms)'
我曾尝试在
中使用标记.id\u,但结果与现在相同
如何更改“tags.id”的查询以检索所需的元素?我通过自定义控制器解决了这个问题,因为事实证明Strapi没有我发现的和逻辑。虽然我不喜欢这样做,但这会占用大量资源,因为数组会被更新和循环很多次。如果有其他建议,请让我知道
我在这里做的是取消限制,以便获得所有实体,然后:
获取具有所有标记的所有实体(使用或),并具有其余过滤器
一旦大小减少了一些东西,我就在元素中循环,并找到拥有所有标记ID的元素
我不想一一经历,所以一旦达到极限,我就打破foreach循环
“严格使用”
const{sanitizentity}=require(“strapi utils”)
module.exports={
异步查找(ctx){
让标签;
让实体;
让tagEntities=[];
让限制;
如果(ctx.query?\u限制){
limit=ctx.query.\U limit;
删除ctx.query.\u限制;
}
if(ctx.query?\u where?.tags){
tags=ctx.query.\u where.tags;
删除ctx.query.\u where.tags;
ctx.query._其中[“tags.id_in”]=标记;
}
if(ctx.query.\u q){
实体=等待strapi.services[“自定义控制器”].search(ctx.query);
}否则{
entities=wait strapi.services[“custom controller”].find(ctx.query);
}
标记实体=[];
试一试{
entities.forEach((元素)=>{
if(limit newListId.push(tag?.id));
var结果=标记?.every((val)=>{
返回newListId.indexOf(val)>=0;
});
如果(!标记){
tagenties.push(元素);
限制--;
}
如果(结果){
tagenties.push(元素);
限制--;
}
});
}捕获(e){}
返回标记实体.map((实体)=>
sanitizeEntity(实体,{model:strapi.models[“custom controller”]})
);
},
};
'GET /api-end-point/count?_where%5Bcategory_contains%5D=houses&_where%5Btags.id%5D%5B0%5D=60420fc8c4bed44584638dec&_where%5Btags.id%5D%5B1%5D=60420febc4bed44584638ded&_where%5Btags.id%5D%5B2%5D=6042151fbea9601908f2c774&_where%5Btags.id%5D%5B3%5D=60421520bea9601908f2c775&_where%5Btags.id%5D%5B4%5D=60421521bea9601908f2c776&_where%5Btags.id%5D%5B5%5D=60421522bea9601908f2c777&_where%5Btags.id%5D%5B6%5D=60421522bea9601908f2c778 (562 ms)'
if (ctx.query?._where?.tags) {
tags = ctx.query._where.tags;
delete ctx.query._where.tags;
ctx.query._where["tags.id_in"] = tags;
}
if (ctx.query._q) {
entities = await strapi.services["custom-controller"].search(ctx.query);
} else {
entities = await strapi.services["custom-controller"].find(ctx.query);
}
tagEntities = [];
try {
entities.forEach((element) => {
if (limit <= 0) throw BreakException;
let newListId = [];
element.tags.forEach((tag) => newListId.push(tag?.id));
var result = tags?.every((val) => {
return newListId.indexOf(val) >= 0;
});
if (!tags) {
tagEntities.push(element);
limit--;
}
if (result) {
tagEntities.push(element);
limit--;
}
});
} catch (e) {}
return tagEntities.map((entity) =>
sanitizeEntity(entity, { model: strapi.models["custom-controller"] })
);
},
};