motiur
motiur

Reputation: 115

express-ejs-layout sometimes fails to detect passing argument

I am rendering a page where I am passing data "userInfo" but sometimes it render and sometimes it shows this ReferenceError, not sure Why? My question is if there have any mistake then it should not work at all. How can I fix it?

<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>
                    <%}%>
          
My js file <pre> 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; } }); //var audioData; 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}); } } }); });

I am passing boolean value via userInfo.

Upvotes: 0

Views: 357

Answers (1)

motiur
motiur

Reputation: 115

I got the answer. @noisypixy you are right my video_Data was not populated correctly. When I render my view sometimes video data was not getting. So I changed my code now it works as expected.



    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});

         }

      }

      });


      }
      });



    });

Upvotes: 0

Related Questions