From bcf391a7d0eab4ac351ee5db5c723cc1cec2248a Mon Sep 17 00:00:00 2001 From: yashrajverma Date: Thu, 20 May 2021 12:14:03 +0530 Subject: [PATCH] Updated! --- controllers/blog.js | 13 +++++-------- models/Blog.js | 2 +- routes/Coupon.js | 2 +- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/controllers/blog.js b/controllers/blog.js index e1e6318..2106169 100644 --- a/controllers/blog.js +++ b/controllers/blog.js @@ -4,10 +4,7 @@ const User = require("../models/User"); module.exports.getAllBlogs = async (req, res, next) => { try { - let blog = await Blog.find().populate( - "author", - "-passwordResetToken -password" - ); + let blog = await Blog.find(); if (blog) { res.json({ blogs: blog }); } else { @@ -22,7 +19,7 @@ module.exports.getAllBlogs = async (req, res, next) => { }; module.exports.addBlog = async (req, res, next) => { - const { _id, title, body, image } = req.body; //_id is of user from frontend who is adding the blog. + const { _id, title, body, image, author } = req.body; //_id is of user from frontend who is adding the blog. try { let user = await User.findById({ _id }); if (!title) { @@ -39,7 +36,7 @@ module.exports.addBlog = async (req, res, next) => { body, image, date: new Date(), - author: user, + author, }); await blog.save(); res.json({ message: "Blog Saved Successfully!" }); @@ -88,12 +85,12 @@ module.exports.editBlog = async (req, res, next) => { res.status(422).json({ message: "Please, Add the Image of the Blog." }); return; } - let blog = Blog.findById(_id); + let blog = await Blog.findById({ _id }); if (blog) { blog.title = title; blog.body = body; blog.image = image; - await blog.save(); + blog = await blog.save(); res.json({ message: "Blog Updated!" }); } else { res.status(422).json({ error: "Blog Doesn't Found" }); diff --git a/models/Blog.js b/models/Blog.js index 774666e..b37cd16 100644 --- a/models/Blog.js +++ b/models/Blog.js @@ -5,7 +5,7 @@ const blogSchema = new mongoose.Schema({ type: String, require: true, }, - author: { ref: "User", type: mongoose.Schema.Types.ObjectId }, + author: { required: true, type: String }, date: { type: Date, }, diff --git a/routes/Coupon.js b/routes/Coupon.js index dc53f24..5c8b41f 100644 --- a/routes/Coupon.js +++ b/routes/Coupon.js @@ -21,7 +21,7 @@ router.post("/set-coupon", (req, res) => { }); } }); -router.get("/getAllCoupons", isAuth, isAdmin, couponController.getAllCoupons); +router.get("/getAllCoupons", couponController.getAllCoupons); router.post("/addCoupon", isAuth, isAdmin, couponController.addCoupon);