remove empty objects from query

This commit is contained in:
Víctor Losada Hernández
2026-11-03 00:02:10 +01:00
parent 15aadc3c0d
commit d7e8ae8d14
2 changed files with 52 additions and 71 deletions
+8 -18
View File
@@ -108,8 +108,8 @@ const VaultPage = (props)=>{
const response = await request.get(`/api/vault?${params}`)
.catch((error)=>{
console.log('error at loadPage: ', error);
setError(error);
console.error('error at loadPage: ', error.response?.body.message);
setError(error.response?.body);
updateStateWithBrews([], 1);
});
@@ -531,23 +531,13 @@ const VaultPage = (props)=>{
}
if(error) {
if(error.HBErrorCode === undefined) {
return (
<div className='foundBrews noBrews'>
<h3>Error: Unknown error</h3>
</div>
);
} else {
const errorText = ErrorIndex()[error.HBErrorCode.toString()] || '';
return (
<div className='foundBrews noBrews'>
<h3>Error: {errorText}</h3>
</div>
);
}
const errorText = error.message || ErrorIndex()[error.HBErrorCode?.toString()] || 'Unknown error';
return (
<div className='foundBrews noBrews'>
<h3>Error: {errorText}</h3>
</div>
);
}
if(!brewCollection) {
+44 -53
View File
@@ -29,59 +29,64 @@ const rendererConditions = (legacy, v3)=>{
return {}; // If all renderers selected, renderer field not needed in query for speed
};
const tagConditions = (tagsAnd, tagsOr, tagsNot) => {
const conditions = {};
const tagConditions = (tagsAnd, tagsOr, tagsNot)=>{
const conditions = {};
if (tagsAnd.length > 0) {
conditions.$and = tagsAnd.map(tag => ({
tags: tag.trim()
}));
}
if(tagsAnd.length > 0) {
conditions.$and = tagsAnd.map((tag)=>({
tags : tag.trim()
}));
}
if (tagsOr.length > 0) {
conditions.$or = tagsOr.map(tag => ({
tags: tag.trim()
}));
}
if(tagsOr.length > 0) {
conditions.$or = tagsOr.map((tag)=>({
tags : tag.trim()
}));
}
if (tagsNot.length > 0) {
conditions.$nor = tagsNot.map(tag => ({
tags: tag.trim()
}));
}
if(tagsNot.length > 0) {
conditions.$nor = tagsNot.map((tag)=>({
tags : tag.trim()
}));
}
return conditions;
return conditions;
};
const sortConditions = (sort, dir) => {
const sortConditions = (sort, dir)=>{
return {
[sort]: dir === 'asc' ? 1 : -1
[sort] : dir === 'asc' ? 1 : -1
};
}
};
const findBrews = async (req, res)=>{
const buildVaultQuery = (req)=>{
const title = req.query.title || '';
const author = req.query.author || '';
const page = Math.max(parseInt(req.query.page) || 1, 1);
const count = Math.max(parseInt(req.query.count) || 20, 10);
const skip = (page - 1) * count;
const tagsAnd = req.query.tagsAnd?.split(',') || [];
const tagsOr = req.query.tagsOr?.split(',') || [];
const tagsNot = req.query.tagsNot?.split(',') || [];
const sort = req.query.sort || 'title';
const dir = req.query.dir || 'asc';
//console.log(`Title: ${title}, Author: ${author}, page: ${page}, count: ${count}, tagsAnd: ${tagsAnd}, tagsOr: ${tagsOr} tagsNot: ${tagsNot}`);
const combinedQuery = {
$and : [
{ published: true },
rendererConditions(req.query.legacy, req.query.v3),
titleConditions(title),
tagConditions(tagsAnd, tagsOr, tagsNot),
authorConditions(author)
],
};
const conditions = [
{ published: true },
rendererConditions(req.query.legacy, req.query.v3),
titleConditions(title),
authorConditions(author),
tagConditions(tagsAnd, tagsOr, tagsNot)
].filter((condition)=>Object.keys(condition).length > 0);
return { $and: conditions };
};
const findBrews = async (req, res)=>{
const page = Math.max(parseInt(req.query.page) || 1, 1);
const count = Math.max(parseInt(req.query.count) || 20, 10);
const skip = (page - 1) * count;
const sort = req.query.sort || 'title';
const dir = req.query.dir || 'asc';
const combinedQuery = buildVaultQuery(req);
const projection = {
editId : 0,
@@ -108,26 +113,12 @@ const findBrews = async (req, res)=>{
res.json({ brews: processedBrews, page });
})
.catch((error)=>{
throw { ...error, message: 'Error finding brews in Vault search', HBErrorCode: 90 };
throw { ...error, message: error.errorResponse?.errmsg || 'Error finding brews in Vault search', HBErrorCode: 90 };
});
};
const findTotal = async (req, res)=>{
const title = req.query.title || '';
const author = req.query.author || '';
const tagsAnd = req.query.tagsAnd?.split(',') || [];
const tagsOr = req.query.tagsOr?.split(',') || [];
const tagsNot = req.query.tagsNot?.split(',') || [];
const combinedQuery = {
$and : [
{ published: true },
rendererConditions(req.query.legacy, req.query.v3),
titleConditions(title),
authorConditions(author),
tagConditions(tagsAnd, tagsOr, tagsNot)
],
};
const combinedQuery = buildVaultQuery(req);
await HomebrewModel.countDocuments(combinedQuery)
.then((totalBrews)=>{
@@ -135,7 +126,7 @@ const findTotal = async (req, res)=>{
res.json({ totalBrews });
})
.catch((error)=>{
throw { ...error, message: 'Error finding brews in Vault search findTotal function', HBErrorCode: 91 };
throw { ...error, message: error.errorResponse?.errmsg || 'Error finding brews in Vault search findTotal function', HBErrorCode: 91 };
});
};