Skip to content

Commit 069204e

Browse files
listing all categories ,blogs and tags
1 parent 34453a5 commit 069204e

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

server/controllers/blog.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,48 @@ exports.list =(req,res)=>{
125125
error: errorHandler(err)
126126
})
127127
}
128+
res.json(data)
128129
})
129130
}
130131

131132
exports.bloglistsallCategoriesTags =(req,res)=>{
132-
133+
let limit = req.body.limit ? parseInt(req.body.limit) : 10
134+
let skip = req.body.skip ? parseInt(req.body.skip) : 0
135+
let blogs
136+
let categories
137+
let tags
138+
Blog.find({}).populate("categories","_id name slug").populate("taglists","_id name slug")
139+
.populate("postedBy","_id name username profile").sort({createdAt: -1}).skip(skip).limit(limit) // second arguments is for particularly pupulating that specific field
140+
.select("_id title slug excerpt categories taglists postedBy createdAt updatedAt")
141+
.exec((err,data)=>{
142+
if (err){
143+
return res.json({
144+
error: errorHandler(err)
145+
})
146+
}
147+
blogs = data // we get all the blogs
148+
// getting all the categories
149+
Category.find({}).exec((err,cat)=>{
150+
if (err){
151+
return res.json({
152+
error: errorHandler(err)
153+
})
154+
}
155+
categories = cat // get all the categories
156+
// getting all the tags
157+
Tag.find({}).exec((err,tagg)=>{
158+
if (err){
159+
return res.json({
160+
error: errorHandler(err)
161+
})
162+
}
163+
tags = tagg
164+
// return all the categories ,tags and the blogs
165+
res.json({blogs,categories,tags,size: blogs.length});
166+
})
167+
})
168+
})
169+
133170
}
134171

135172
exports.read =(req,res)=>{

0 commit comments

Comments
 (0)