Tuesday, 15 October 2019

Help with TypeError on app with nested pages

I am making a site that is a mock forum where the user clicks on a forum topic, goes to a unique .ejs page with forums, and then goes to another unique .ejs page where the forum is, where both pages are altered based on what the user fills into a Form on the previous page. The first step worked, but I keep getting either a TypeError "can't read property name of null" or a reference error (depending on what object info I type into the .ejs page) when I end up clicking on a link that would go from the "forum topic" page to the "forum". Data is being added to the database and I created corresponding object/variable schemas that are linked to each other and the main app so I'm not sure what the issue is.I'm using this with express and mongodb/mongoose, the page that's getting the type error is a .ejs page. I can't show all of the code in this one post, I'm assuming the main issue is in one of the routing blocks below in the main app document and somewhere in the middle? What is the deal with nested schemas and pages for those who are more experienced with this?Thanks!var express = require("express"); var app = express(); var bodyParser = require("body-parser"); var mongoose = require("mongoose"); var ForumTopic = require("./models/forumtopic"); var Forum = require("./models/forum"); var Reply = require("./models/reply"); app.use(bodyParser.urlencoded({extended: true})); var port = process.env.PORT || 3000; mongoose.connect("mongodb://localhost:port/forumcenter", {useNewUrlParser: true}); mongoose.set('useNewUrlParser', true); app.set("view engine", "ejs"); app.get("/", function(req, res) { res.render("landing"); }); app.get("/main", function(req, res){ ForumTopic.find({}, function(err, allForumTopics){ if(err){ console.log(err); } else { res.render("forumhead", {forumtopics: allForumTopics}); } }); }); app.post("/main", function(req, res){ var name = req.body.name; var forums = req.body.forums; var newForumTopic = {name: name, forums: forums}; ForumTopic.create(newForumTopic, function(err, newlyCreated){ if(err){ console.log(err); } else { res.redirect("/main"); } }); }); app.get("/main/new", function(req, res){ res.render("new"); }); app.get("/main/:id", function(req, res) { ForumTopic.findById(req.params.id).populate("forums").exec(function(err, foundForumTopic) { if(err){ console.log(err); } else { console.log(foundForumTopic); res.render("forumtopic", {forumtopic: foundForumTopic}); } }); }); //// app.get("/main/:id/forum", function(req, res){ Forum.find({}, function(err, allForums){ if(err){ console.log(err); } else { res.render("forumtopic", {forums: allForums}); } }) }); app.post("/main/:id/forum", function(req, res){ ForumTopic.findById(req.params.id, function(err, forumtopic){ if(err){ console.log(err); res.redirect("/main"); } else { //console.log(req,body.comment); Forum.create(req.body.forum, function(err, forum){ if(err){ console.log(err); } else { forumtopic.forums.push(forum); forumtopic.save(); res.redirect('/main/' + forumtopic._id); } }) } }) }); app.get("/main/:id/forum/new", function(req, res){ ForumTopic.findById(req.params.id, function(err, forumtopic){ if(err){ console.log(err); } else { res.render("newforum", {forumtopic: forumtopic}); } }) }); app.get("/main/:id/:forumid", function(req, res) { /// Forum.findById(req.params.id).populate("replies").exec(function(err, foundForum) { if(err){ console.log(err); } else { console.log(foundForum); res.render("forum", {forum: foundForum}); } }) }); ////everything past here probably isn't where the current issue is, .ejs page needs to work first app.get("/main/:id/:forumid/reply/new", function(req, res){ Forum.findById(req.params.id, function(err, forum){ if(err){ console.log(err); } else { res.render("newreply", {forum: forum}); } }) }); app.post("/main/:id/:forumid/reply", function(req, res){ Forum.findById(req.params.id, function(err, forum){ if(err){ console.log(err); res.redirect("/main"); } else { Reply.create(req.body.reply, function(err, reply){ if(err){ console.log(err); } else { forum.replies.push(reply); forum.save(); res.redirect('/main/' + forumtopic._id + "/" + forum._id); } }) } }) }); app.listen(port, function () { console.log("the forumcenter server has Started!"); });

Submitted October 15, 2019 at 01:24PM by thesanemansflying

No comments:

Post a Comment