Skip to content

Commit

Permalink
fix:home render issue
Browse files Browse the repository at this point in the history
  • Loading branch information
arjuncvinod committed Aug 28, 2024
1 parent 3c43e9b commit 9ab1dc3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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("<script>alert('user already exits');window.location.href = '/'</script>");

Expand Down
6 changes: 4 additions & 2 deletions src/postdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

0 comments on commit 9ab1dc3

Please sign in to comment.