Javascript 无法在hbs中显示MySql记录
我试图在我的hbs页面上显示MySql记录,nodejs既没有得到任何错误,也没有得到任何结果。 我使用的是express generator,我在编程,在很长一段时间之后,我在MySsql表中插入了数据。我的下一步是在hbs页面上显示数据,我现在放弃了。我需要你们的帮助,让我知道我的代码中哪里有问题? 谢谢 index.jsJavascript 无法在hbs中显示MySql记录,javascript,mysql,node.js,express,crud,Javascript,Mysql,Node.js,Express,Crud,我试图在我的hbs页面上显示MySql记录,nodejs既没有得到任何错误,也没有得到任何结果。 我使用的是express generator,我在编程,在很长一段时间之后,我在MySsql表中插入了数据。我的下一步是在hbs页面上显示数据,我现在放弃了。我需要你们的帮助,让我知道我的代码中哪里有问题? 谢谢 index.js const { response } = require('express'); var express = require('express'); var router
const { response } = require('express');
var express = require('express');
var router = express.Router();
var mysql = require('mysql')
//SQL connection
var conn = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'MyDatabaseforMyProjects@',
database: 'crud_db'
})
conn.query('SELECT 1 + 1 AS solution', function (err, rows, fields) {
if (err) throw err
console.log('Connected to MySQL..')
})
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index');
});
// create page route
router.get('/create', function(req, res, next) {
res.render('create');
console.log(req.body)
});
// create page route
router.post('/create', function(req, res, next) {
let data = {
first_name: req.body.first_name,
last_name: req.body.last_name,
email : req.body.email,
password: req.body.password,
phone: req.body.phone,
};
conn.query(`INSERT INTO user_form set ?`, [data], function(err, result){
if(err) {console.log(err)}
else{
return res.redirect('/');
}
} )
});
// this script to fetch data from MySQL databse table
router.post('/', function(req, res, next) {
conn.query('SELECT * FROM user_form', function (err, result, data) {
if (err) throw err;
// return res.redirect('/');
});
});
module.exports = router;
<h3 class="text-center border border-light p-5 container col-sm-5">User's Record</h3>
<div class=" container col-sm-6"> Click <a href="/create">here</a> to Register user</div>
<table class="table container col-sm-8">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">First name</th>
<th scope="col">Last name</th>
<th scope="col">Email</th>
<th scope="col">Password</th>
<th scope="col">Phone</th>
<th scope="col">Action</th>
</tr>
<tbody>
{{#each }}
<tr>
<td scope="col">{{ id }}</td>
<td scope="col">{{ first_name }}</td>
<td scope="col">{{ last_name }}</td>
<td scope="col">{{ email }}</td>
<td scope="col">{{ password }}</td>
<td scope="col">{{ phone }}</td>
</tr>
{{/each}}
</tbody>
</thead>
指数.hbs
const { response } = require('express');
var express = require('express');
var router = express.Router();
var mysql = require('mysql')
//SQL connection
var conn = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'MyDatabaseforMyProjects@',
database: 'crud_db'
})
conn.query('SELECT 1 + 1 AS solution', function (err, rows, fields) {
if (err) throw err
console.log('Connected to MySQL..')
})
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index');
});
// create page route
router.get('/create', function(req, res, next) {
res.render('create');
console.log(req.body)
});
// create page route
router.post('/create', function(req, res, next) {
let data = {
first_name: req.body.first_name,
last_name: req.body.last_name,
email : req.body.email,
password: req.body.password,
phone: req.body.phone,
};
conn.query(`INSERT INTO user_form set ?`, [data], function(err, result){
if(err) {console.log(err)}
else{
return res.redirect('/');
}
} )
});
// this script to fetch data from MySQL databse table
router.post('/', function(req, res, next) {
conn.query('SELECT * FROM user_form', function (err, result, data) {
if (err) throw err;
// return res.redirect('/');
});
});
module.exports = router;
<h3 class="text-center border border-light p-5 container col-sm-5">User's Record</h3>
<div class=" container col-sm-6"> Click <a href="/create">here</a> to Register user</div>
<table class="table container col-sm-8">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">First name</th>
<th scope="col">Last name</th>
<th scope="col">Email</th>
<th scope="col">Password</th>
<th scope="col">Phone</th>
<th scope="col">Action</th>
</tr>
<tbody>
{{#each }}
<tr>
<td scope="col">{{ id }}</td>
<td scope="col">{{ first_name }}</td>
<td scope="col">{{ last_name }}</td>
<td scope="col">{{ email }}</td>
<td scope="col">{{ password }}</td>
<td scope="col">{{ phone }}</td>
</tr>
{{/each}}
</tbody>
</thead>
用户记录
单击以注册用户
#
名字
姓
电子邮件
密码
电话
行动
{{{#各}
{{id}
{{first_name}}
{{last_name}}
{{email}}
{{密码}}
{{phone}}
{{/每个}}