渲染模板时,user直接显示 user is not defined
发布于 2 年前 作者 Rudy24 1990 次浏览 来自 问答

渲染模板时,user直接显示 user is not defined,如果把user改成 locals.user 后虽然不会报错,但是locals.user一直是空值, 而我在下面index.js文件console出来确实有值。刚开始学node,希望大家帮个忙,谢谢

  <div class="ui buttons">
    <div class="ui floating dropdown button">
      <i class="icon bars"></i>
      <div class="menu">
        <% if (user) { %>
          <a class="item" href="/posts?author=<%= user._id %>">个人主页</a>
          <div class="divider"></div>
          <a class="item" href="/posts/create">发表文章</a>
          <a class="item" href="/signout">登出</a>
        <% } else { %>
          <a class="item" href="/signin">登录</a>
          <a class="item" href="/signup">注册</a>
        <% } %>
      </div>
    </div>
  </div>
</div> 


ReferenceError: E:\study\demo\Node\myblog\views\posts.ejs:1
 >> 1| <%- include('header') %>

    2| 这是主页

    3| <%- include('footer') %>

E:\study\demo\Node\myblog\views\header.ejs:13
    11|   <body>

    12|   <%- include('components/nav') %>

 >> 13|   <%- include('components/nav-setting') %>

    14|   <%- include('components/notification') %>

E:\study\demo\Node\myblog\views\components\nav-setting.ejs:6
    4|       <i class="icon bars"></i>

    5|       <div class="menu">

 >> 6|         <% if (user) { %>

    7|           <a class="item" href="/posts?author=<%= user._id %>">个人主页</a>

    8|           <div class="divider"></div>

    9|           <a class="item" href="/posts/create">发表文章</a>


user is not defined
    at eval (eval at compile (E:\study\demo\Node\myblog\node_modules\[email protected]@ejs\lib\ejs.js:549:12), <anonymous>:11:8)
    at returnedFn (E:\study\demo\Node\myblog\node_modules\[email protected]@ejs\lib\ejs.js:580:17)
    at include (E:\study\demo\Node\myblog\node_modules\[email protected]@ejs\lib\ejs.js:578:39)
    at eval (eval at compile (E:\study\demo\Node\myblog\node_modules\[email protected]@ejs\lib\ejs.js:549:12), <anonymous>:17:17)
    at returnedFn (E:\study\demo\Node\myblog\node_modules\[email protected]@ejs\lib\ejs.js:580:17)
    at include (E:\study\demo\Node\myblog\node_modules\[email protected]@ejs\lib\ejs.js:578:39)
    at eval (eval at compile (E:\study\demo\Node\myblog\node_modules\[email protected]@ejs\lib\ejs.js:549:12), <anonymous>:9:17)
    at returnedFn (E:\study\demo\Node\myblog\node_modules\[email protected]@ejs\lib\ejs.js:580:17)
    at tryHandleCache (E:\study\demo\Node\myblog\node_modules\[email protected]@ejs\lib\ejs.js:223:34)
    at View.exports.renderFile [as engine] (E:\study\demo\Node\myblog\node_modules\[email protected]@ejs\lib\ejs.js:437:10)


\n```
主文件index.js

// 添加模板必需的三个变量
app.use(function (req, res, next) {
  res.locals.user = req.session.user;
  console.log(res.locals.user);
  res.locals.success = req.flash('success').toString();
  res.locals.error = req.flash('error').toString();
  next();
});
1 回复
// 添加模板必需的三个变量
app.use(function (req, res, next) {
  res.locals.user = req.session.user
  res.locals.success = req.flash('success').toString()
  res.locals.error = req.flash('error').toString()
  next()
})

// 正常请求的日志
app.use(expressWinston.logger({
  transports: [
    new (winston.transports.Console)({
      json: true,
      colorize: true
    }),
    new winston.transports.File({
      filename: 'logs/success.log'
    })
  ]
}))
// 路由
routes(app)

你的路由是写在// 添加模板必需的三个变量之后吗,主文件index.js

回到顶部