0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-06 01:22:44 +00:00

edit console logs and change totalDocs logic to count correctly

This commit is contained in:
Víctor Losada Hernández
2024-02-12 23:54:00 +01:00
parent d233e2b4a5
commit 534131d994

View File

@@ -9,7 +9,7 @@ const archive = {
try { try {
const title = req.query.title || ''; const title = req.query.title || '';
const page = parseInt(req.query.page) || 1; const page = parseInt(req.query.page) || 1;
console.log('try:', page); console.log('trying page ', page);
const pageSize = 10; // Set a default page size const pageSize = 10; // Set a default page size
const skip = (page - 1) * pageSize; const skip = (page - 1) * pageSize;
@@ -17,29 +17,28 @@ const archive = {
title : { $regex: decodeURIComponent(title), $options: 'i' }, title : { $regex: decodeURIComponent(title), $options: 'i' },
published : true published : true
}; };
const projection = { const projection = {
editId : 0, editId : 0,
googleId : 0, googleId : 0,
text : 0, text : 0,
textBin : 0, textBin : 0,
}; };
const brews = await HomebrewModel.find(titleQuery, projection) const brews = await HomebrewModel.find(titleQuery, projection)
.skip(skip) .skip(skip)
.limit(pageSize) .limit(pageSize)
.maxTimeMS(5000) .maxTimeMS(5000)
.exec(); .exec();
console.log(brews.length);
if(!brews || brews.length === 0) { if(!brews || brews.length === 0) {
// No published documents found with the given title // No published documents found with the given title
return res.status(404).json({ error: 'Published documents not found' }); return res.status(404).json({ error: 'Published documents not found' });
} }
const totalDocuments = await HomebrewModel.countDocuments(titleQuery, projection);
const totalDocuments = await HomebrewModel.countDocuments(title);
const totalPages = Math.ceil(totalDocuments / pageSize); const totalPages = Math.ceil(totalDocuments / pageSize);
console.log('Total brews: ', totalDocuments);
console.log('Total pages: ', totalPages);
return res.json({ brews, page, totalPages }); return res.json({ brews, page, totalPages });
} catch (error) { } catch (error) {
console.error(error); console.error(error);