title: MySql update after inserting nodejs
body: <p>I made a simple app in nodejs with express framework and ejs template where I can insert into mysql database and see the results, but for some reasons when I insert and refresh the page, the new date is not showing, even if it puts it in the database.I need to close the server to see the new data.</p>
  
  <p>My index.js route file looks like this:</p>
  
  <pre><code>var express = require('express');
  var mysql = require('mysql');
  var router = express.Router();
  
  var users = [];
  var age = [];
  
  var connection = mysql.createConnection( {
      host: 'localhost',
      user: 'admin',
      password: 'pass',
      database: 'users'
  });
  
  console.log('\nconnecting to database');
  connection.connect();
  
  var query = connection.query('select * from users', function(err, result) {
      if(err) {
          console.error(err);
          return;
      } else {
          for(var i = 0; i&lt;result.length; i++)
          {
              users.push(result[i]['user']);
              age.push(result[i]['age']);
          }
      }
  });
  
  
  
  /* GET home page. */
  router.get('/', function(req, res, next) {
    res.render('index', { 
      title: 'Index page',
      users: users,
      age: age
    });
  });
  
  
  /* GET insert page. */
  router.post('/insert', function(req, res, next) {
      var user = req.body.user;
      var age = req.body.age;
  
      var user_p = {
          user: user,
          age: age
      };
  
      console.log("post received: %s %s", user, age);
  
      var query = connection.query('insert into users set ?', user_p, function(err, result) {
          if(err) {
              console.error(err);
              return;
          } else {
              console.log(result);
          }
      });
  
      res.render('insert', { 
          title: 'Insert page',
          user: user_p
      });
  });
  // I've removed this connection.end() after I saw the error. But still not getting the new data out of the database when I go back to the index file
  connection.end();
  
  module.exports = router;
  </code></pre>
  
  <p>The index.ejs: </p>
  
  <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd"&gt;
  &lt;html&gt;
      &lt;head&gt;&lt;% include partials/template/head.ejs %&gt;&lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;&lt;%= title %&gt;&lt;/h1&gt;
          &lt;p&gt;Welcome to &lt;%= title %&gt;&lt;/p&gt;
  
          &lt;ul&gt;
              &lt;% for(var i=0; i&lt;users.length; i++) {%&gt;
                  &lt;li&gt;User: &lt;%= users[i] %&gt;, Age: &lt;%= age[i] %&gt;&lt;/li&gt;
              &lt;% } %&gt;
          &lt;/ul&gt;
  
  
          &lt;form action="/insert" method="post"&gt;
              &lt;input type="text" name="user" placeholder="User"/&gt;
              &lt;input type="text" name="age" placeholder="Age"/&gt;
              &lt;input type="submit" value="send"/&gt;
          &lt;/form&gt;
  
  
      &lt;% include partials/template/footer.ejs %&gt;
      &lt;% console.log('included footer'); %&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and insert.ejs:</p>
  
  <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd"&gt;
  &lt;html&gt;
      &lt;head&gt;&lt;% include partials/template/head.ejs %&gt;&lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;&lt;%= title %&gt;&lt;/h1&gt;
          &lt;p&gt;Welcome to &lt;%= title %&gt;&lt;/p&gt;
  
      &lt;p&gt;User: &lt;%= user.user %&gt;&lt;/p&gt;
      &lt;p&gt;Age: &lt;%= user.age %&gt;&lt;/p&gt;
  
      &lt;% include partials/template/footer.ejs %&gt;
      &lt;% console.log('included footer'); %&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>How can I see "live" the changes in my database after I insert and refresh?</p>
  
  <p>P.S.
  I just saw that I have the following error in my console:</p>
  
  <pre><code>{ [Error: Cannot enqueue Query after invoking quit.] code: 'PROTOCOL_ENQUEUE_AFTER_QUIT', fatal: false }
  </code></pre>
  
  <p>P.P.S.
  I've removed this connection.end() after I saw the error. But still not getting the new data out of the database when I go back to the index file</p>