title: GET data from MongoDB and display on the front end
body: <p>I´ve just started learning NodeJS to use as a back-end of my applications, but this is my first time working with a server-side technology.</p>
  
  <p>I am trying to build a simple app which is a form where I want to insert the data into the database (mongodb) and get the data to display on the front end. I am using express-handlebars.</p>
  
  <p>The data is successfully being saved into the database, however when I am trying to get the data from the database to display on the front end nothing happens.</p>
  
  <p>Does anyone knows what may be wrong with my code ? </p>
  
  <p>Thank you!! </p>
  
  <p>//code begins here</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var express = require('express');
  var router = express.Router();
  var mongoose = require('mongoose');
  var assert = require('assert');
  
  var url = 'mongodb://felipe:teste@ds033125.mlab.com:33125/form';
  mongoose.connect(url);
  
  
  /* GET home page. */
  router.get('/', function(req, res, next) {
    res.render('index');
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>// INSERT DATA INTO DATABASE(MONGODB)</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>router.post('/insert', function(req, res, next) {
      
      var Schema = mongoose.Schema;
      
      var itemSchema = new Schema({
          nome: String,
          sobrenome: String,
          nacionalidade: String,
          email: String
      });
      
      var CreateItem = mongoose.model('CreateItem', itemSchema);
      
      var item = new CreateItem({
          
          nome: req.body.getNome,
          sobrenome: req.body.getSobrenome,
          nacionalidade: req.body.getNacionalidade,
          email: req.body.getEmail
      });
      
      item.save(function(err) {
          if (err) throw err;
          console.log(item.nome + ' was saved!');
      });
      
      
      
      res.redirect('/');
      console.log(item.nome + ' ' + item.sobrenome + ' was saved!'); 
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>// GET DATA FROM MONGODB AND DISPLAY ON THE FRONT END</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>router.get('/', function(req, res, next) {
    res.render('index');
  });
  
  
  router.get('/get-data', function(req, res, next) {
      
      var resultArray = [];
      
      mongoose.connect(url, function(err, db) {
          assert.equal(null, err);
          var cursor = db.collection('createitems').find();
          cursor.forEach(function(err, doc) {
              assert.equal(null, err);
              resultArray.push(doc); 
          }, function() {
              db.close;
              res.render('index', {items: resultArray});
          });
      });
      
      res.redirect('/');
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>// HTML where the data will be displayed</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>{{# each items}}
        &lt;tr&gt;                    
           &lt;div class="col-md-3"&gt;
              &lt;td class="data"&gt;{{ this.nome }}&lt;/td&gt;
           &lt;/div&gt;
           &lt;div class="col-md-3"&gt;
              &lt;td class="data"&gt;{{ this.sobrenome }}&lt;/td&gt;
           &lt;/div&gt;
           &lt;div class="col-md-3"&gt;
              &lt;td class="data"&gt;{{ this.nacionalidade }}&lt;/td&gt;
           &lt;/div&gt;
           &lt;div class="col-md-3"&gt;
              &lt;td class="data"&gt;{{ this.email }}&lt;/td&gt;
           &lt;/div&gt;
        &lt;/tr&gt;
                      
  {{/each}}</code></pre>
  </div>
  </div>
  </p>