I'm trying to run 2 queries against a MySQL database and they are not working. And display it, getting errors, can I do this a better way? I'm mainly asking how to display it in the home.handlebars file and how to make more than 1 query from MySQL database.
var express = require('express');
var handlebars = require('express-handlebars');
var bodyParser = require('body-parser')
var mysql = require('mysql');
var app = express();
app.engine('handlebars', handlebars({defaultLayout: 'main'}));
app.set('views', __dirname + '/views');
app.set('view engine', 'handlebars');
app.use(express.static('public'));
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'translation_project'
});
connection.connect();
app.get('/', function(req, res) {
var translatorid = 45;
var sqlQuery = "SELECT title,type FROM itemtable;"
connection.query(sqlQuery,function(err,rows,fields){
if(err){
console.log('Error'+err);
}else{
res.render('home',{items:rows}); // to be displayed in the home page
}
});
var sqlQuery2 = "SELECT dateoftranslation FROM translateditems;"
connection.query(sqlQuery2,function(err,rows,fields){
if(err){
console.log('Error'+err);
}else{
res.render('home',{dates:rows}); // to be displayed in the home page
}
});
});
app.listen(3000, function() {
console.log('Server is running at port 3000');
});
and Here is the home.handlebars file
<h1> My Applied Items </h1>
{{#each items}} <!-- the items refreneced in the index page-->
<p>{{title}}</p>
<p>{{type}}</p>
{{/each}}
{{#each dates}} <!-- the dates refreneced in the index page-->
<p>{{dateoftranslation}}</p>
{{/each}}
and Here is the home.handlebars file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Users Profile</title>
</head>
<body>
<h1> Mohamed </h1>
{{{body}}}
</body>
</html>