我正在创建一个模拟论坛的网站,用户单击一个论坛主题,转到具有论坛的唯一.ejs页面,然后转到该论坛所在的另一个唯一.ejs页面,这两个页面都是根据用户在上一页中填写的表单进行了更改。第一步是有效的,但是当我最终单击一个链接时,我总是收到TypeError“无法读取null的属性名称”或引用错误(取决于我在.ejs页面中键入的对象信息)。从“论坛主题”页面转到“论坛”。数据正在添加到数据库中,并且我创建了相互链接的猫鼬模式,这些模式与主应用程序相互链接,所以我不确定问题出在哪里。
我将它与express和mongodb / mongoose一起使用,出现类型错误的页面是.ejs页面。我假设主要问题是在主应用程序文档中的一个路由块中,但可能不是,可能是我没有得到的其他概念或页面配置。对于那些对此有经验的人来说,如何处理嵌套的模式和页面?
以下是页面本身出现错误:
TypeError: /Users/roberts4/project_4/views/forum.ejs:4
2|
3| <nav class = "container">
>> 4| <a href = "#">Welcome to Forum/ <%= forum.name %></a>
5| <a href = "#">Reply</a>
6| <a class = "btn btn-primary btn-sm" href="/main/<%= forumtopic._id
%>/<%= forum._id %>/reply/new">Add New Forum</a>
7|
Cannot read property 'name' of null
您将看到的post函数在“ forumtopic” id的内部创建了“ forum”,这与我发布“ forumtopic”本身时所做的不同。我尝试添加req.body变量,但似乎仅在您不将其发布到其他页面时使用。
///there is code above this for forumtopic that's similar, except the
////.post block has the .create by itself and not inside of .findById
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});
}
})
});
///there is code below this that's not accessed yet because of the error
获取TypeError'无法读取属性名称为null',期望 一个页面,显示基于app.js中节点的信息。