title: How to Pass an Array of Objects (Node.JS) to an EJS file
body: <p>I am trying to create a form. You enter your info, hit submit, and then a new page opens showing you what you submitted. There's also a button at the bottom that will redirect to a new page with all of the different contact information you entered.</p>
  
  <p>I am having a problem passing a variable from my JavaScript file to my EJS file (formserver.js to contacts.ejs). I keep getting an error that my array of objects has not been passed (line 16 of contacts.ejs). </p>
  
  <p>I was wondering if anyone could help explain why my Object array is not being passed into the EJS file, and what I could do to remedy that.</p>
  
  <p>Here is the code: </p>
  
  <p>formserver.js</p>
  
  <pre><code>var connect = require("connect");
  var logger = require("morgan");
  var http = require("http");
  var ejs = require('ejs');
  var bodyparse = require('body-parser');
  
  
  var app = connect()
      .use (logger('dev'))
      .use (bodyparse())
      .use (serve);
  
  http.createServer(app).listen(3000);
  
  var allInfo = new Array;
  
  function serve(req, res)
  {
      //console.log("Entered ", req.url);
      var gender = req.body.gender;
      var firstName = req.body.firstName;
      var lastName = req.body.lastName;
      var street = req.body.street;
      var city = req.body.city;
      var state = req.body.state;
      var zip = req.body.zip;
      var phone = req.body.phone;
      var email = req.body.email;
      var contact = req.body.contact;
      var mail = req.body.mail;
  
      var form = {gender: gender, fname : firstName, lname : lastName, street : street, 
              city : city, state: state, zip: zip, phone : phone, email: email, mail: mail }
  
  
  
      if (req.url == "/mailer")
      {
  
          })
  }
  </code></pre>
  
  <p>contacts.ejs</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;p&gt; &lt;% allInfo %&gt; &lt;/p&gt; 
  &lt;table&gt;
      &lt;tr&gt;
          &lt;th&gt;Name&lt;/th&gt;
          &lt;th&gt;Address&lt;/th&gt;
          &lt;th&gt;Phone&lt;/th&gt;
          &lt;th&gt;Email&lt;/th&gt;
          &lt;th&gt;Contact by mail?&lt;/th&gt;
      &lt;/tr&gt;
  
      &lt;% for (var person in allInfo) { %&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;%= allInfo[i].gender %&gt;. &lt;%= allInfo[i].fname %&gt; &lt;%= allInfo[i].lname %&gt;&lt;/td&gt;              
      &lt;/tr&gt;
  
      &lt;tr&gt;
          &lt;td&gt; &lt;%= street %&gt;, &lt;%= city %&gt;, &lt;%= state %&gt; &lt;%= zip %&gt;&lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
          &lt;td&gt;&lt;%= phone %&gt;&lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
          &lt;td&gt;&lt;%= email %&gt;&lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
          &lt;td&gt;&lt;%= mail %&gt;&lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;% } %&gt;
  &lt;/table&gt; 
  </code></pre>
  
  <p></p>
  
  <p></p>
  
  <p>mailer.ejs</p>
  
  <pre><code>    &lt;!DOCTYPE html&gt;
      &lt;html&gt;
      &lt;body&gt;
      &lt;form action = "/submit" method = "post"&gt;
          &lt;fieldset style = "width:500px"&gt;
          &lt;p&gt;
              &lt;div&gt;
              &lt;input type = "radio" name = "gender" value = "Mr"&gt; Mr. 
              &lt;input type = "radio" name = "gender" value = "Mrs"&gt; Mrs. 
              &lt;input type = "radio" name = "gender" value = "Ms"&gt; Ms. 
              &lt;input type = "radio" name = "gender" value = "Dr"&gt; Dr. 
              &lt;/div&gt;
          &lt;/p&gt;
          &lt;p&gt;
          &lt;div&gt;
              &lt;label  for = "first"&gt;First Name: &lt;/label&gt;
              &lt;input type = "text" name = "firstName" required&gt;
              &lt;label for = "last"&gt;Last Name:&lt;/label&gt;
              &lt;input type = "text" name = "lastName" required&gt;
          &lt;/div&gt;
          &lt;/p&gt;
          &lt;p&gt;
          &lt;div&gt;
              &lt;label for = "street" &gt; Street: &lt;/label&gt;
              &lt;input type = "text" name = "street" location = "street"&gt;
              &lt;label for = "city" name = "city"&gt; City: &lt;/label&gt;
              &lt;input type = "text" name = "city" location = "city"&gt;
          &lt;/div&gt;
          &lt;/p&gt;
          &lt;p&gt;
          &lt;div&gt;
              &lt;label id = "state" for = "state" name = "state"&gt;State: &lt;/label&gt;
              &lt;select name = "state" id = "state"&gt;
                  &lt;option value = "" disabled = "disabled" selected = "selected"&gt; &lt;/option&gt;
                  &lt;option value = "NJ""&gt; NJ &lt;/option&gt;
                  &lt;option value = "NY"&gt; NY &lt;/option&gt;
                  &lt;option value = "PA"&gt; PA &lt;/option&gt;
                  &lt;option value = "CT"&gt; CT &lt;/option&gt;
              &lt;/select&gt;
              &lt;label for = "zip"&gt; Zip: &lt;/label&gt;
              &lt;input type = "text" name = "zip" location = "zip"&gt;
          &lt;/div&gt;
          &lt;/p&gt;
  
          &lt;p&gt;
          &lt;div&gt;
              &lt;label for = "phone" &gt;Phone:&lt;/label&gt;
              &lt;input type = "text" name = "phone" location = "phone"&gt;
          &lt;/div&gt;
          &lt;/p&gt;
  
          &lt;p&gt;
          &lt;div&gt;
              &lt;label for = "email" &gt;Email:&lt;/label&gt;
              &lt;input type = "text" name = "email" location = "email"&gt;
          &lt;/div&gt;
          &lt;/p&gt;
  
          &lt;p&gt;
          &lt;div&gt;
              &lt;label for = "contact" name = "contact"&gt; How may we contact you? &lt;/label&gt;
              &lt;input type = "checkbox" name = "contact" value = "yPhone"&gt; Phone &lt;/input&gt;
              &lt;input type = "checkbox" name = "contact" value = "mail"&gt; Mail &lt;/input&gt;
              &lt;input type = "checkbox" name = "contact" value = "yEmail"&gt; Email &lt;/input&gt;
              &lt;input type = "checkbox" name = "contact" value = "any" checked&gt; Any &lt;/input&gt;
          &lt;/div&gt;
          &lt;/p&gt;
          &lt;/fieldset&gt;
  
          &lt;div&gt;
              &lt;button type = "submit" name = "submit" value = "submit"&gt; Send me spam forever! &lt;/button&gt;
          &lt;/div&gt;
      &lt;/form&gt;
          &lt;p&gt;&lt;a href = "/contacts"&gt; View list of contacts! &lt;/a&gt;&lt;/p&gt;
      &lt;/body&gt;
      &lt;/html&gt;
  
  submit.ejs
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt; 
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div&gt;&lt;strong&gt; Contact Information Submitted &lt;/strong&gt;&lt;/div&gt;
      &lt;div&gt;&lt;strong&gt; Name: &lt;%= gender %&gt;. &lt;%= fname %&gt; &lt;%= lname %&gt; &lt;/strong&gt;&lt;/div&gt;
      &lt;div&gt;&lt;strong&gt; Address: &lt;%= street %&gt;, &lt;%= city %&gt;, &lt;%= state %&gt; &lt;%= zip %&gt;&lt;/strong&gt;&lt;/div&gt;
      &lt;div&gt;&lt;strong&gt; Contact by Phone: &lt;%= phone %&gt; &lt;/strong&gt;&lt;/div&gt;
      &lt;div&gt;&lt;strong&gt; Contact by Mail: &lt;%= mail %&gt;&lt;/strong&gt;&lt;/div&gt;
      &lt;div&gt;&lt;strong&gt; Contact by Email: &lt;%= email %&gt; &lt;/strong&gt;&lt;/div&gt;
  
  
      &lt;p&gt;&lt;a href = "/contacts"&gt; View list of contacts! &lt;/a&gt;&lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>