提问者:小点点

express-ejs-layout有时无法检测传递参数


我正在呈现一个页面,其中我正在传递数据“userinfo”,但有时它呈现,有时它显示这个引用错误,不确定为什么?我的问题是,如果有任何错误,那么它应该是完全不起作用的。我该怎么修好它?

<pre>  

    ReferenceError: /Users/user/Desktop/iSlamic/views/layout.ejs:77
       75|                     </li>
       76|                     
    >> 77|                          if(userInfo)
       78|                  <li class="dropdown">
       79|                         <a href="#" class="dropdown-toggle" data-toggle="dropdown">Motiur<b class="caret"></b></a>
       80|                         <ul class="dropdown-menu">

    userInfo is not defined
       at eval (eval at <anonymous> (/Users/user/Desktop/iSlamic/node_modules/ejs/lib/ejs.js:491:12), <anonymous>:11:10)
       at returnedFn (/Users/user/Desktop/iSlamic/node_modules/ejs/lib/ejs.js:520:17)
       at View.exports.renderFile [as engine] (/Users/user/Desktop/iSlamic/node_modules/ejs/lib/ejs.js:374:31)
       at View.render (/Users/user/Desktop/iSlamic/node_modules/express/lib/view.js:126:8)
       at tryRender (/Users/user/Desktop/iSlamic/node_modules/express/lib/application.js:639:10)
       at EventEmitter.render (/Users/user/Desktop/iSlamic/node_modules/express/lib/application.js:591:3)
       at ServerResponse.render (/Users/user/Desktop/iSlamic/node_modules/express/lib/response.js:961:7)
       at /Users/user/Desktop/iSlamic/node_modules/express-ejs-layouts/lib/express-layouts.js:108:14
       at View.exports.renderFile [as engine] (/Users/user/Desktop/iSlamic/node_modules/ejs/lib/ejs.js:379:10)
       at View.render (/Users/user/Desktop/iSlamic/node_modules/express/lib/view.js:126:8)
       at tryRender (/Users/user/Desktop/iSlamic/node_modules/express/lib/application.js:639:10)
       at EventEmitter.render (/Users/user/Desktop/iSlamic/node_modules/express/lib/application.js:591:3)
       at ServerResponse.render (/Users/user/Desktop/iSlamic/node_modules/express/lib/response.js:961:7)
       at ServerResponse.res.render (/Users/user/Desktop/iSlamic/node_modules/express-ejs-layouts/lib/express-layouts.js:77:12)
       at /Users/user/Desktop/iSlamic/app.js:97:7
       at Layer.handle_error (/Users/user/Desktop/iSlamic/node_modules/express/lib/router/layer.js:71:5)}

</pre>


   
<%if(userInfo){%>
                 <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Motiur<b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            <li>
                                <a href="/admin/insert">Insert</a>
                            </li>
                            <li>
                                <a href="/admin/update">Update</a>
                            </li>
                            <li>
                                <a href="/admin/delete">Delete</a>
                            </li>
                            <li>
                                <a  href="/logout">Logout</a> 
                            </li>
                        </ul>
                    </li>
                     <% }else{ %>  
                     <li>
                      <a  href="/admin/login">Login</a> 
                      </li>
                    <%}%>
          

null

我正在通过userinfo传递布尔值。


共1个答案

匿名用户

我找到答案了。@noisypixy您是对的,我的video_Data没有正确填充。当我呈现我的视图时,有时视频数据没有得到。所以我改变了我的代码,现在它像预期的那样工作。



    router.get('/speaker/:name', function(req, res, next) {

      var video_Data;

      was.find({"name":req.params.name, "wasType":"video"}).sort({_id:-1}).limit(5).exec(function(err,videoDocs){

      if(err)
      {
        res.json(err)
              mongoose.connection.close();
      }
      else
      {

        video_Data = videoDocs;

        was.find({"name":req.params.name, "wasType":"audio"}).sort({_id:-1}).limit(5).exec(function(err,docs){
        if(err)
      {
              res.json(err);
              mongoose.connection.close();
      }
      else
      {
        if(video_Data==null&&docs==null)

         {
          res.json("Data does not exist");
         }else{
          res.render('video2', {"data":docs,"videoData": video_Data,"userInfo":req.session.admin});

         }

      }

      });


      }
      });



    });