Javascript 为什么Jade不显示日期值?
翡翠模板是:Javascript 为什么Jade不显示日期值?,javascript,node.js,pug,Javascript,Node.js,Pug,翡翠模板是: block content h1= title div.container if notes each item in notes div h3 Date: #{item.date} p=item.content else div No notes 但是,仅呈现标题和项目.内
block content
h1= title
div.container
if notes
each item in notes
div
h3 Date: #{item.date}
p=item.content
else
div No notes
但是,仅呈现标题
和项目.内容
,而项目.日期
仅呈现空的
标记。传递给Jade的对象是:
{
'title': 'Notes',
'notes': [{"content":"A note","date":"2014-03-30T12:03:50.096Z"}]
}
我从中得到:
mongoose.connect('mongodb://localhost/notes');
var db = mongoose.connection;
db.on('error', function(err) {
console.log('Error in connecting to MongoDb: ' + err);
});
db.once('open', function() {
Notes
.find()
.select('-_id')
.exec(function(err, retrievedNotes) {
console.log(JSON.stringify(retrievedNotes));
res.render('index', {'title': 'Notes', 'notes': retrievedNotes});
mongoose.disconnect();
});
});
然而,只有:
<h1>Notes</h1>
<div class="container">
<div>
<h3>Date: </h3>
<p>A note</p>
</div>
</div>
注释
日期:
便条
已呈现。我认为您在打开数据库连接和启动服务器时遇到了一些问题。下面的代码适合我
app.get('/', function(req, res) {
Notes
.find()
.select('-_id')
.exec(function(err, retrievedNotes) {
console.log(JSON.stringify(retrievedNotes));
res.render('index', {
'title': 'Notes',
'notes': retrievedNotes
});
});
});
db.once('open', function() {
console.log('connected to db')
http.createServer(app).listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
});
您还应该
mongoose.disconnect()代码>当您关闭服务器时,因为如果您在路由处理程序中断开连接,那么您将无法处理其他请求,因为您只打开了db
连接一次。我意识到这是因为我在数据库架构中创建的字段是dateCreated
而不是date
!我所要做的就是将{item.date}
更改为{item.dateCreated}
我看不出jade的代码有任何问题。对我来说,它运行正常。@Pio是的,我在在线jade编译器上试用过,它确实有效。。。Mabey它与Node.js代码有关-我已经添加了这一点,奇怪的是,我console.log
编辑了包含值的变量,它看起来很好