Undifined property

  arrays, javascript, object, undefined

The console.log(posts) is printing values correctly but the key1 is declared undefined even if the hole array is printing just fine.

This the script code and below is the Ejs file contaning parsing of data, The key is declared undefined.

 const posts = [];   //array
 app.post("/compose",function(req,res){
 const post = {
     key1: req.body.content1,
     content: req.body.content2
              };
 posts.push(post);
 res.redirect("/");
  }); 



       <h1>Home</h1>
      <%- include("partials/header") -%>
      <%= homeStartingContent %>

   <% for(var i=0;i<=posts.length;i++){ %>
         <% console.log(posts[i].key1); %>
  <%}%>[enter image description here][1]


   <%- include("partials/footer") -%>


     [1]: https://i.stack.imgur.com/skfB4.png

Source: Ask Javascript Questions

LEAVE A COMMENT