我正在尝试使用Express建立一个基本博客,当我重新阅读我的节目页面时,我遇到了麻烦。当我在浏览器中打开路径时,页面会按预期显示,但我在控制台中收到一条错误,指出"标题未定义"。
这是有问题的路线:
app.get("/blog/:id", function(req, res){
Blog.findById(req.params.id, function(err, foundBlog){
// if(err){
// console.log(err);
// } else {
console.log(req.params.id);
console.log(foundBlog);
res.render("show", {blog: foundBlog});
// }
});
});
show.ejs给我带来了问题:
<div class="container">
<div class="row">
<div class="container">
<h1 style="text-align: center"><%= blog.title %></h1></h1>
</div>
<div style="width: 70%; margin: 25px auto;">
<p>Written by: <%= blog.author %></p>
</div>
<div style="width: 70%; margin: 25px auto;">
<p><%= blog.desc %></p>
</div>
</div>
<p>
<a class="btn btn-success" href="/blog/<%= blog._id %>/edit">Edit</a>
</p>
<a href="/blog">Back</a>
</div>
我的Mongo架构:
var blogSchema = new mongoose.Schema({
author: String,
title: {type: String, required: true},
desc: String,
posted: {type: Date, default: Date.now()}
});
var Blog = mongoose.model("Blog", blogSchema)
提前致谢!
以前的Console.log结果:
{ _id: 5a2341c79dd8680dc676969d,
author: 'Jacob',
title: 'Getting erors',
desc: 'Why am I getting errors on the new page?',
__v: 0,
posted: 2017-12-03T00:13:00.609Z }
end of console
undefined
end of console
我现在注意到,当我运行节点app.js时,我自己的控制台正在做一些奇怪的事情。我在第二次显示我在运行应用程序时点击了回车键。我已经清除了控制台,但显示的信息又开始了。现在两个console.log都没有显示出来。如果重要的话,这是在云端9上。
threads13:~/workspace $ ls
JacobBlog/ README.md client/ node_modules/ package.json server.js
threads13:~/workspace $ cd JacobBlog/
threads13:~/workspace/JacobBlog (master) $ ls
app.js collections data node node_modules/ package.json public/ views/
threads13:~/workspace/JacobBlog (master) $ node app.js
(node:2192) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/connections.html#use-mongo-client
The Jacob Blog Server Has Started!
TypeError: /home/ubuntu/workspace/JacobBlog/views/show.ejs:10
8| <div class="row">
9| <div class="container">
>> 10| <h1 style="text-align: center"><%= blog.title %></h1></h1>
11| </div>
12|
13| <div style="width: 70%; margin: 25px auto;">
Cannot read property 'title' of undefined
at eval (eval at compile (/home/ubuntu/workspace/JacobBlog/node_modules/ejs/lib/ejs.js:549:12), <anonymous>:22:30)
发现在我的HTML文件中出现了一些小错误导致了这个问题。谢谢!