Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么Jade不显示日期值?_Javascript_Node.js_Pug - Fatal编程技术网

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
编辑了包含值的变量,它看起来很好