Karatay Karatay - 2 months ago 6
MySQL Question

Nodejs Website doesn't display second mysql table value

I have a express website that display values in the mysql. It displays the news but it doesn't display events. I know for sure that the events table has 2 values in it.

here is my index.js file in the routes:

var express = require('express');
var router = express.Router();
var mysql = require('mysql');

//connection variables
var connection = mysql.createConnection({
host: 'localhost',
port: '3306',
user: 'root',
password: 'mypassword*',
database: 'icomnodejs'

});

connection.connect();

router.get('/', function (req, res, next) {

connection.query('SELECT * FROM news', function (err, rows, fields) {
if (err) throw err;
res.render('index', {
'news' : rows
});
});
});

router.get('/', function (req, res, next) {
connection.query('SELECT * FROM events', function (err, rows, fields) {
if (err) throw err;
res.render('index', {
'events': rows
});
});
});

module.exports = router;


And here is my view file called index.handlebars

<div class="row">
{{#if news}}
{{#each news}}

<div class="col-md-4 img-portfolio">
<a href="portfolio-item.html">
<img class="img-responsive img-hover" src="http://placehold.it/700x400" alt="">
</a>
<h3>
<a href="portfolio-item.html">{{title}}</a>
</h3>
<p>{{text}}</p>
</div>
{{/each}}
</div>

{{else}}
<p>No News</p>
{{/if}}

<hr>

<!-- Marketing Icons Section -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
Upcoming Events Lopping As well
</h1>
</div>

{{#if events}}
{{#each events}}
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h4><i class="fa fa-fw fa-check"></i> {{title}}</h4>
</div>
<div class="panel-body">
<p>{{text}}</p>
<a href="#" class="btn btn-default">Learn More</a>
</div>
</div>
</div>
{{/each}}
{{else}}
<p>No events</p>
{{/if}}

</div>


Based on the view file, I see news from the database but I don't see events. I just don't see where the problem is.

Answer

I think that you misunderstood the concept of express middlewares and routes. You are registering against the same path different router handlers. Something like that will do what you need:

var express = require('express');
var router = express.Router();
var mysql = require('mysql');

//connection variables
var connection = mysql.createConnection({
  host: 'localhost',
  port: '3306',
  user: 'root',
  password: 'mypassword*',
  database: 'icomnodejs'

});

connection.connect();

function getNews(req, res, next) {
  connection.query('SELECT * FROM news', function (err, rows, fields) {
    if (err) {
      return next(err);
    };

    req._news = rows;
    return next();
  });
}

function getEvents(req, res, next) {
  connection.query('SELECT * FROM events', function (err, rows, fields) {
    if (err) {
      return next(err);
    };

    req._events = rows;
    return next();
  });
}

router.get('/', getNews, getEvents, function (req, res, next) {
  res.render('index', {
    'news': req._news,
    'events': req._events
  });
});

module.exports = router;