【问题标题】:javascript what's the different between this two pieces of code?javascript 这两段代码有什么不同?
【发布时间】:2013-03-06 10:59:19
【问题描述】:

我是 javascript 和节点的新手。 第一个:

app.get('/', function(req, res){
  articleProvider.findAll(function(error, docs){
      res.render('index.jade', { locals: {
        title: 'Blog',
        articles:docs
        }
      });
  });
});

第二次

app.get('/', function(req, res){
  articleProvider.findAll(function(error, docs){
      res.render('index.jade', {
        title: 'Blog',
        articles:docs
        }
      );
  });
});

第一个代码来自本教程http://howtonode.org/express-mongodb,但不起作用。 第二个代码确实有效,我只是不知道为什么?其他部分完全相同。

【问题讨论】:

  • 所以你基本上是在问locals 是干什么用的?
  • 哇...你是对的。做得很好,真的很难看到。
  • “不起作用”是什么意思?你得到什么错误?
  • 是的,我在发布后大约 9/10 秒内捕捉到了它。并在我删除它之前注意到加一;)

标签: javascript node.js express


【解决方案1】:

在 Express 的早期版本中,您需要将想要提供给 Jade 模板的变量放在 locals 参数的 res.render 字段中。

这在 3.x 中发生了变化,因此该参数的所有字段都可以作为局部变量用于呈现的模板。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 2011-05-09
    相关资源
    最近更新 更多