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}`) const response = await request.get(`/api/vault?${params}`)
.catch((error)=>{ .catch((error)=>{
console.log('error at loadPage: ', error); console.error('error at loadPage: ', error.response?.body.message);
setError(error); setError(error.response?.body);
updateStateWithBrews([], 1); updateStateWithBrews([], 1);
}); });
@@ -531,23 +531,13 @@ const VaultPage = (props)=>{
} }
if(error) { if(error) {
if(error.HBErrorCode === undefined) { const errorText = error.message || ErrorIndex()[error.HBErrorCode?.toString()] || 'Unknown error';
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>
);
}
return (
<div className='foundBrews noBrews'>
<h3>Error: {errorText}</h3>
</div>
);
} }
if(!brewCollection) { 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 return {}; // If all renderers selected, renderer field not needed in query for speed
}; };
const tagConditions = (tagsAnd, tagsOr, tagsNot) => { const tagConditions = (tagsAnd, tagsOr, tagsNot)=>{
const conditions = {}; const conditions = {};
if (tagsAnd.length > 0) { if(tagsAnd.length > 0) {
conditions.$and = tagsAnd.map(tag => ({ conditions.$and = tagsAnd.map((tag)=>({
tags: tag.trim() tags : tag.trim()
})); }));
} }
if (tagsOr.length > 0) { if(tagsOr.length > 0) {
conditions.$or = tagsOr.map(tag => ({ conditions.$or = tagsOr.map((tag)=>({
tags: tag.trim() tags : tag.trim()
})); }));
} }
if (tagsNot.length > 0) { if(tagsNot.length > 0) {
conditions.$nor = tagsNot.map(tag => ({ conditions.$nor = tagsNot.map((tag)=>({
tags: tag.trim() tags : tag.trim()
})); }));
} }
return conditions; return conditions;
}; };
const sortConditions = (sort, dir) => { const sortConditions = (sort, dir)=>{
return { 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 title = req.query.title || '';
const author = req.query.author || ''; 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 tagsAnd = req.query.tagsAnd?.split(',') || [];
const tagsOr = req.query.tagsOr?.split(',') || []; const tagsOr = req.query.tagsOr?.split(',') || [];
const tagsNot = req.query.tagsNot?.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}`); //console.log(`Title: ${title}, Author: ${author}, page: ${page}, count: ${count}, tagsAnd: ${tagsAnd}, tagsOr: ${tagsOr} tagsNot: ${tagsNot}`);
const combinedQuery = { const conditions = [
$and : [ { published: true },
{ published: true }, rendererConditions(req.query.legacy, req.query.v3),
rendererConditions(req.query.legacy, req.query.v3), titleConditions(title),
titleConditions(title), authorConditions(author),
tagConditions(tagsAnd, tagsOr, tagsNot), tagConditions(tagsAnd, tagsOr, tagsNot)
authorConditions(author) ].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 = { const projection = {
editId : 0, editId : 0,
@@ -108,26 +113,12 @@ const findBrews = async (req, res)=>{
res.json({ brews: processedBrews, page }); res.json({ brews: processedBrews, page });
}) })
.catch((error)=>{ .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 findTotal = async (req, res)=>{
const title = req.query.title || ''; const combinedQuery = buildVaultQuery(req);
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)
],
};
await HomebrewModel.countDocuments(combinedQuery) await HomebrewModel.countDocuments(combinedQuery)
.then((totalBrews)=>{ .then((totalBrews)=>{
@@ -135,7 +126,7 @@ const findTotal = async (req, res)=>{
res.json({ totalBrews }); res.json({ totalBrews });
}) })
.catch((error)=>{ .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 };
}); });
}; };