search

Home  >  Q&A  >  body text

javascript - 切换页面,为什么我routes里面req.session.userid存在,却无法在客户端上显示<%=userid%>的值?

迷茫迷茫2873 days ago388

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 16:34:40

    The userid is given in roomview.html: roomview.html里给了userid

    res.render('roomview', {
        userid: req.session.userid,
        rooms:docs
    });

    addroom.html的路由里你没写:

    res.render('addroom.html')

    所以自然找不到。

    如果你希望ejs里可以直接获取session里的信息,可以尝试使用locals,比如前置一个中间件:

    app.use(function(req, res, next) {
      res.locals.userid = req.session.userid;
      next();
    });

    这样,你的ejs模版里就可以直接使用<%= userid %> rrreee

    And you didn’t write in the route of addroom.html: 🎜 rrreee 🎜So naturally I can’t find it. 🎜 🎜If you want ejs to directly obtain the information in session, you can try to use locals, such as prefixing a middleware: 🎜 rrreee 🎜In this way, you can directly use <%= userid %> in your ejs template🎜

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 16:34:40

    The pictures I drew are quite beautiful

    reply
    0
  • Cancelreply