handle all not found errors

This commit is contained in:
Amir Mohamadi
2021-12-31 17:01:17 +03:30
parent 5c07791225
commit 064d642b7e
47 changed files with 3386 additions and 3238 deletions
+15 -9
View File
@@ -80,6 +80,9 @@ module.exports.create = [
.cover(thumbWith, thumbHeight)
.write(`./static/uploads/images/blog/thumb_${coverName}`)
})
.catch(err => {
console.log(err)
})
const data = {
post_details: {
@@ -112,7 +115,7 @@ module.exports.create = [
module.exports.getAll = [
(req, res) => {
BlogPost.find({}).sort({_id: -1}).exec((err, posts) => {
if (err) console.log(err)
if (err) return res.status(500).json({message: err})
return res.json(posts)
})
}
@@ -122,7 +125,7 @@ module.exports.getAll = [
module.exports.getOne = [
(req, res) => {
BlogPost.findById(req.params.id, (err, post) => {
if (err) console.log(err)
if (err) return res.status(404).json({message: err})
return res.json(post)
})
}
@@ -213,10 +216,13 @@ module.exports.update = [
.cover(thumbWith, thumbHeight)
.write(`./static/uploads/images/blog/thumb_${coverName}`)
})
.catch(err => {
console.log(err)
})
}
BlogPost.findByIdAndUpdate(req.params.id, data, (err, oldData) => {
if (err) console.log(err)
if (err) return res.status(404).json({message: err})
if (cover && cover.data) {
fs.unlink(`./static/${oldData.cover}`, err => {
if (err) console.log(err)
@@ -225,8 +231,8 @@ module.exports.update = [
if (err) console.log(err)
})
}
BlogPost.findById(oldData._id, (err, newData) => {
if (err) console.log(err)
BlogPost.findById(oldData._id, (err1, newData) => {
if (err1) return res.status(404).json({message: err1})
return res.json(newData)
})
})
@@ -238,11 +244,11 @@ module.exports.delete = [
(req, res) => {
BlogPost.findByIdAndDelete(req.params.id, (err, post) => {
if (err) return res.status(404).json({message: err})
fs.unlink(`./static/${post.cover}`, err => {
if (err) console.log(err)
fs.unlink(`./static/${post.cover}`, err1 => {
if (err1) console.log(err1)
})
fs.unlink(`./static/${post.thumb}`, err => {
if (err) console.log(err)
fs.unlink(`./static/${post.thumb}`, err2 => {
if (err2) console.log(err2)
})
return res.json({message: v_m['fa'].response.success_remove})
})