Fix homepage API 500 by using axios baseURL and stable paginate.
deploy to danak / build_and_deploy (push) Has been cancelled
deploy to danak / build_and_deploy (push) Has been cancelled
Replace self-proxy with direct SSR baseURL, rewrite blog post pagination with async/await, pin mongoose-paginate-v2, and add API error logging. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -112,25 +112,39 @@ module.exports.getAll = [
|
||||
// '/api/public/blogPosts?limit=4' --> gets only 4 published post of all categories
|
||||
// '/api/public/blogPosts?category=${encodeURIComponent(categoryTitle)}&page=${pageNumber}' --> gets posts depend on the category title and in chunked data
|
||||
|
||||
if (req.query.category) {
|
||||
try {
|
||||
const categoryID = await BlogCategory.findOne({$or: [{'locale.fa.title': req.query.category}, {'locale.en.title': req.query.category}]})
|
||||
const filter = req.query.category === 'all' ? {published: true} : {published: true, category: categoryID._id}
|
||||
BlogPost.paginate(filter, {page: req.query.page, limit: blogPaginateLimit, populate: 'category', sort: {_id: -1}}, (err, posts) => {
|
||||
if (err) return res500(res, {message: _sr['fa'].not_found.item_id})
|
||||
return res.json(posts)
|
||||
try {
|
||||
if (req.query.category) {
|
||||
const filter = {published: true}
|
||||
if (req.query.category !== 'all') {
|
||||
const category = await BlogCategory.findOne({
|
||||
$or: [
|
||||
{'locale.fa.title': req.query.category},
|
||||
{'locale.en.title': req.query.category}
|
||||
]
|
||||
})
|
||||
if (!category) return res404(res, _sr['fa'].not_found.item_id)
|
||||
filter.category = category._id
|
||||
}
|
||||
const posts = await BlogPost.paginate(filter, {
|
||||
page: Number(req.query.page) || 1,
|
||||
limit: blogPaginateLimit,
|
||||
populate: 'category',
|
||||
sort: {_id: -1}
|
||||
})
|
||||
} catch (e) {
|
||||
return res500(res, e)
|
||||
return res.json(posts)
|
||||
}
|
||||
} else {
|
||||
let query = {published: true}
|
||||
|
||||
const query = {published: true}
|
||||
if (req.query.getAll) delete query.published
|
||||
const limit = Number(req.query.limit) || false
|
||||
BlogPost.find(query).sort({_id: -1}).limit(limit).populate('category').exec((err, posts) => {
|
||||
if (err) return res500(res, err)
|
||||
else return res.json(posts)
|
||||
})
|
||||
const limit = Number(req.query.limit) || 0
|
||||
const posts = await BlogPost.find(query)
|
||||
.sort({_id: -1})
|
||||
.limit(limit)
|
||||
.populate('category')
|
||||
return res.json(posts)
|
||||
} catch (e) {
|
||||
console.error('blogPost getAll error:', e)
|
||||
return res500(res, e)
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user