Skip to content

Commit e439cc5

Browse files
showing realted blogs by creating blogListRelated routes and controller
1 parent f64d33a commit e439cc5

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

server/controllers/blog.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,23 @@ exports.photo =(req,res)=>{
269269
res.set('Content-Type',blog.photo.contentType)
270270
return res.send(blog.photo.data);
271271
})
272-
};
272+
};
273+
274+
exports.blogListRelated=(req,res)=>{
275+
let limit = req.body.limit ? parseInt(req.body.limit) : 3
276+
277+
const {_id,categories} = req.body.blog
278+
279+
// not including _id but including categories
280+
// while showing related blogs excluding the blog itself and showing other blogs instead
281+
Blog.find({_id: {$ne: _id},categories: {$in: categories}}).limit(limit).populate('postedBy','_id name profile').select('slug excerpt postedBy createdAt updatedAt')
282+
.exec((err,blogs)=>{
283+
if (err){
284+
return res.status(400).json({
285+
error: "Blog not found"
286+
})
287+
res.json(blogs)
288+
}
289+
})
290+
291+
}

0 commit comments

Comments
 (0)