3

I'm having trouble understanding how to fetch data from the MongoDB database and display it on HTML. I already have set for the data.

this is the the server.js file.

const path = require('path');
const express = require('express');
const bodyParser = require('body-parser')
const mongoose = require('mongoose');
const app = express();


//map global promise - get rid of warning
mongoose.Promise = global.Promise;

// connect to  mongoose
mongoose.connect('mongodb://localhost/peppino-calc', {
  useMongoClient: true
})
.then(() => { console.log('MongoDB connected...')})
.catch(err => console.log(err));

//Load salaryModel
require('./modles/Idea.js');
const Idea = mongoose.model('ideas');


//body parser middleware
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())


// post history page
app.get('/history', (req, res) => {
  Idea.find({})
  .sort({date:'desc'})
  res.sendFile(__dirname + '/js/newJs/history.html')
})

 //process form
 app.post('/ideas', (req, res) => {
   let errors = [];
   if(errors.length > 0) {
     console.log(errors[0]);
   } else {
    const newUser = {
       amount: req.body.totalamount,
       hours: req.body.totalhours,
       salary: req.body.totalsalary,
       tip: req.body.totaltip,
       date: req.body.datetotal
     }
     new Idea(newUser)
     .save()
     .then(idea => {
       res.redirect('/history');
     })
   }
 });

 app.use(express.static(path.join(__dirname, './js/newJs')));
 app.set('port', process.env.PORT || 5700);

 var server = app.listen(app.get('port'), function() {
   console.log('listening on port ', server.address().port);
 });

my goal is to display the data from the database in a specific html page. any help?

1
  • 3
    Uhm, what is the problem? Commented Jan 8, 2018 at 12:55

1 Answer 1

0

You have to use a template engine in order to display data in an html page, there are many template engines, you can choose one from this link

Here is an example using pug:

1- install pug

npm install pug --save

2- set view directory:

app.set('views', path.join(__dirname, 'views'));

3- set pug as the default view engine

app.set('view engine', 'pug');

4- create history.pug inside views folder

doctype html
html
    head
    body
        table
            thead
                tr
                    th Name
                    th date
            tbody
                each idea in ideas
                    tr
                        td= idea.name
                        td= idea.date

5- pass data from express to pug:

app.get('/history', (req, res) => {
    let ideas = Idea.find({})
    .sort({date:'desc'}).exec( (err, ideas) => {
        res.render('history', ideas);
    });
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.