EJS data not defined where not needed

Viewed 9

My app works fine. But this error just hangs around. I've tried passing data to different calls in my app, but it remains to reference this one page (admin.ejs) which actually renders and works when I go to the admin page.

I will receive the error even when I am on a different page like index.ejs or visitor.ejs

For the love of everything holy why am I stupid?

error on console:

ReferenceError: C:\southernOklahomaPride\views\admin.ejs:42
    40|     </thead>      
    41|   <tbody>
 >> 42|     <% for(i=0; i<data.length;i++){%>       
    43|       <tr>        
    44|        <td><form action="/visitor"  method="post"><button class="delRegister" name="buttonID"value="<%=data[i].email%>" type="submit"><%= data[i].name %></button></form></td>
    45|          <td><button> <a href="mailto:<%=data[i].email%>">email</a></button></td>

data is not defined       
    at eval (eval at compile (C:\southernOklahomaPride\node_modules\ejs\lib\ejs.js:673:12), <anonymous>:27:19)
    at admin (C:\southernOklahomaPride\node_modules\ejs\lib\ejs.js:703:17)    
    at tryHandleCache (C:\southernOklahomaPride\node_modules\ejs\lib\ejs.js:274:36)
    at View.exports.renderFile [as engine] (C:\southernOklahomaPride\node_modules\ejs\lib\ejs.js:491:10)
    at View.render (C:\southernOklahomaPride\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\southernOklahomaPride\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\southernOklahomaPride\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\southernOklahomaPride\node_modules\express\lib\response.js:1008:7)     
    at C:\southernOklahomaPride\app.js:47:7
    at Layer.handle_error (C:\southernOklahomaPride\node_modules\express\lib\router\layer.js:71:5)      
GET /images/sook_foot.png 200 34.081 ms - 107665    
ReferenceError: C:\southernOklahomaPride\views\admin.ejs:42
s\express\lib\response.js:1008:7)        
    at C:\southernOklahomaPride\app.js:47:7    at Layer.handle_error (C:\southernOklahomaPride\node_modules\express\lib\router\layer.js:71:5)

admin.js

router.get('/admin', (req,res) =>{
   async function gettingEmails(){
    try {
     await client.connect();
     await getEmails(client);
    }
    catch(err){
      console.log(err);
    }
    finally{
    await client.close();
  }}
 gettingEmails().catch(console.error);

  async function getEmails(client){
   const data = await client.db(dbName).collection('registry').find().toArray();
   const blogs= await client.db(dbName).collection('blogs').find().toArray();
   const socialData = await client.db(dbName).collection('social').find().toArray();
   const socialPlat = await client.db(dbConfig).collection('platforms').find().toArray();

   res.render('admin', {title:'Admin Page',socialPlat:socialPlat, data:data, blogs:blogs, socialData:socialData});
   }
  })

admin.ejs

 <table>
    <thead>
   <th>name</th><th>email</th>
    </thead>
  <tbody>
    <% for(i=0; i<data.length;i++){%>
      <tr>
       <td><form action="/visitor"  method="post"><button class="delRegister" name="buttonID"value="<%=data[i].email%>" type="submit"><%= data[i].name %></button></form></td>
         <td><button> <a href="mailto:<%=data[i].email%>">email</a></button></td>
          <td><form action=""><button>archive</button></form></td>
     </tr>
     <% } %>
   </tbody>
  </table>

index.js

router.get('/',(req, res, next)=> {
  async function gettingBlogs(){
    try {
     await client.connect();
     await getBlogs(client);
    }
    catch(err){
      console.log(err)
    }
    finally{
    await client.close();
  }}
  //calling the function
  gettingBlogs().catch(console.error);
  async function getBlogs(client){

   // const dataStr = await client.db(dbName).collection('registry').find({"type": {$in:['registry']}}).toArray();
    const data = await client.db(dbName).collection('blogs').aggregate([{$sort:{"postDate":-1}}]).toArray();
     const socialData = await client.db(dbName).collection('social').find().toArray();
    const socialPlat = await client.db(dbConfig).collection('platforms').find().toArray();
    res.render('index', {title:'Welcome', data:data, socialData:socialData, socialPlat:socialPlat})
    }
});

index.ejs

<html>
    <%- include('config/head.ejs')%>
<body>
 
    <canvas id="bkgdCanvas"></canvas>  
<div id="backgroundFX">
 
   
    <%- include('config/header.ejs')%>

    <div class="mainStyle">

     <div class="landingPage">   
        <div class="imgBox"><img class="mainImg"src="images/sook_banner.png" alt=""></div>
        <br><br>
        <div class="imgBox"><a href="register"><img class="mainImg" src="images/sook_foot.png" alt=""></a></div>
<% for(var i=0;i<data.length;i++){%>
    <div class="eventBody">
<h1><%= data[i].bTitle%></h1>
<h2><%= data[i].bSubtitle%></h2>
<p><%= data[i].bDetails%></p>

<div class="imgBox"><img class="mainImg"src="<%= data[i].imgName%>" alt=""></div>
</div>
<%}%>
</div>

</div>
<%- include('config/footer.ejs')%>
</div>
<!--script src="javascripts/backgroundFX1.js"></script-->
</body>
 </html>
0 Answers
Related