Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET data from MongoDB and display on the front end

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.

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.

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.

Does anyone knows what may be wrong with my code ?

Thank you!!

//code begins here

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var assert = require('assert');

var url = 'mongodb://felipe:[email protected]:33125/form';
mongoose.connect(url);


/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index');
});

// INSERT DATA INTO DATABASE(MONGODB)

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!'); 
});

// GET DATA FROM MONGODB AND DISPLAY ON THE FRONT END

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('/');
});

// HTML where the data will be displayed

{{# each items}}
      <tr>                    
         <div class="col-md-3">
            <td class="data">{{ this.nome }}</td>
         </div>
         <div class="col-md-3">
            <td class="data">{{ this.sobrenome }}</td>
         </div>
         <div class="col-md-3">
            <td class="data">{{ this.nacionalidade }}</td>
         </div>
         <div class="col-md-3">
            <td class="data">{{ this.email }}</td>
         </div>
      </tr>
                    
{{/each}}
like image 908
Felipe Paifer Avatar asked Nov 08 '22 13:11

Felipe Paifer


1 Answers

do not use this: {{ this.nome }} just use it like that: {{ nome }}

like image 173
Reca Designland Avatar answered Nov 14 '22 21:11

Reca Designland