Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.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_Session_Express_Pug - Fatal编程技术网

Javascript 在JADE中访问会话变量

Javascript 在JADE中访问会话变量,javascript,node.js,session,express,pug,Javascript,Node.js,Session,Express,Pug,我的GET/new结束点如下所示: router.get('/new', function(req, res) { console.log(JSON.stringify(req.session.application)); //<-- This prints FINE res.render('new', { title: 'Add a new intent' }); }); 当我在控制台上打印req.session.application对象时,它打印得很好,但是

我的
GET/new
结束点如下所示:

router.get('/new', function(req, res) {
  console.log(JSON.stringify(req.session.application)); //<-- This prints FINE

  res.render('new', { 
    title: 'Add a new intent'
  });
});

当我在控制台上打印
req.session.application
对象时,它打印得很好,但是当呈现
new.jade
时,它没有从会话中找到
应用程序
对象,并认为它是
空的
。我错过了什么

为模板提供变量

res.render('new', { 
  // <- here is all variables that will be available in jade
  title: 'Add a new intent',
  application: req.session.application
});
res.render('new',{

//我最后编写了一个quick midleware函数,该函数检查会话中是否存在
应用程序
对象,并将其添加到会话对象中,如下所示。通过此更改,在渲染Jade视图时,我不需要将
req.session.application
对象传递到
res.render()
方法

app.js中的中间件:

app.use(function(req, res, next) {

  if (req.session && req.session.application) {
    mongoose.model('Application').findOne({ _id: req.session.application._id }, function(err, application) {
      if (application) {
        req.application = application;
        req.session.application = application;  //refresh the session value
        res.locals.application = application;
      }
      // finishing processing the middleware and run the route
      next();
    });
  } else {
    next();
  }
});
...
h1 #{application.name}
...
来自Jade view的片段:

app.use(function(req, res, next) {

  if (req.session && req.session.application) {
    mongoose.model('Application').findOne({ _id: req.session.application._id }, function(err, application) {
      if (application) {
        req.application = application;
        req.session.application = application;  //refresh the session value
        res.locals.application = application;
      }
      // finishing processing the middleware and run the route
      next();
    });
  } else {
    next();
  }
});
...
h1 #{application.name}
...

但是,如果我直接想从jade访问会话变量,而不是创建一个局部变量,该怎么办?
res.render('new',{session:req.session});