Skip to content

Fix comment schema #781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .lh/.lhignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# list file to not track by the local-history extension. comment line starts with a '#' character
# each line describe a regular expression pattern (search for 'Javascript regex')
# it will relate to the workspace directory root. for example:
# '.*\.txt' ignores any file with 'txt' extension
# '/test/.*' ignores all the files under the 'test' directory
# '.*/test/.*' ignores all the files under any 'test' directory (even under sub-folders)
1 change: 1 addition & 0 deletions binary-upload-boom
Submodule binary-upload-boom added at 2681d1
14 changes: 6 additions & 8 deletions config/database.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
const mongoose = require("mongoose");
const mongoose = require('mongoose');

const connectDB = async () => {
console.log('MONGO_URI:', process.env.MONGO_URI); // Debugging line
try {
const conn = await mongoose.connect(process.env.DB_STRING, {
const conn = await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true,
});

console.log(`MongoDB Connected: ${conn.connection.host}`);
} catch (err) {
console.error(err);
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}
};

module.exports = connectDB;
module.exports = connectDB;
19 changes: 19 additions & 0 deletions controllers/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const cloudinary = require("../middleware/cloudinary");
const Post = require("../models/Post");
const Comment = require("../models/Comment");

module.exports = {
createComment: async (req, res) => {
try {
await Comment.create({
comment: req.body.comment,
likes: 0,
post: req.params.id,
});
console.log("Comment has been added!");
res.redirect("/post/" + req.params.id);
} catch (err) {
console.log(err);
}
},
};
4 changes: 3 additions & 1 deletion controllers/posts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const cloudinary = require("../middleware/cloudinary");
const Post = require("../models/Post");
const Comment = require("../models/Comment");

module.exports = {
getProfile: async (req, res) => {
Expand All @@ -21,7 +22,8 @@ module.exports = {
getPost: async (req, res) => {
try {
const post = await Post.findById(req.params.id);
res.render("post.ejs", { post: post, user: req.user });
const comments = await Comment.find( {post: req.params.id }).sort({ createdAt: "desc" }).lean();
res.render("post.ejs", { post: post, user: req.user}, { comments: comments });
} catch (err) {
console.log(err);
}
Expand Down
22 changes: 22 additions & 0 deletions models/Comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const mongoose = require("mongoose");

const CommentSchema = new mongoose.Schema({
comment: {
type: String,
required: true, // Fix the typo here
},
likes: {
type: Number,
required: true,
},
post: {
type: mongoose.Schema.Types.ObjectId,
ref: 'post',
},
createdAt: {
type: Date,
default: Date.now,
},
});

module.exports = mongoose.models.Comment || mongoose.model("Comment", CommentSchema);
1 change: 1 addition & 0 deletions my-binary-upload-boom
Submodule my-binary-upload-boom added at 6ac3c3
Loading