diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..30bc162 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/node_modules \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 2d2689e..33a4070 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "main-project", + "name": "Blogging-Website", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/src/app.js b/src/app.js index 753ad7b..b3b81da 100644 --- a/src/app.js +++ b/src/app.js @@ -43,24 +43,24 @@ const visitSchema = new mongoose.Schema({ const visits = mongoose.model("visits", visitSchema); -app.get("/home", (req, res) => { - +app.get("/home", async (req, res) => { if (req.session.useremail) { - PosT.find((err, results) => { - req.session.result = results; - }); + try { + const posts = await PosT.find().exec(); + const sortedPosts = await PosT.find().sort({ like: "desc" }).exec(); - PosT.find((err, result) => { - // console.log(req.session.sortedresult); res.render("home", { user: req.session.username, - posts: req.session.result, + posts: posts, date: Date.now(), - sposts: result, + sposts: sortedPosts, }); - }).sort({ like: "desc" }); + } catch (err) { + console.log(err); + res.render("error", { error: "Error fetching posts" }); + } } else { - res.redirect("/") + res.redirect("/"); } }); app.get("/", (req, res) => { @@ -105,7 +105,7 @@ app.post("/signup", async (req, res) => { await Profile.insertMany(profileData) req.session.useremail = req.body.email; req.session.username = req.body.name; - res.redirect("/home"); + res.redirect("home"); }else{ res.send(""); diff --git a/src/postdb.js b/src/postdb.js index 0fc1958..8e8cd38 100644 --- a/src/postdb.js +++ b/src/postdb.js @@ -8,8 +8,9 @@ async function main() { { useNewUrlParser: true } ); // mongoose.connect("mongodb://127.0.0.1:27017/myblog") for local DB + console.log("post connected"); } -main(); +main() const postSchema = new mongoose.Schema({ author: String, title: String, @@ -19,7 +20,8 @@ const postSchema = new mongoose.Schema({ like:Number, likedby:[String] }); + const PosT = mongoose.model("post", postSchema); -module.exports = PosT +module.exports = PosT \ No newline at end of file