【发布时间】:2021-06-06 08:37:17
【问题描述】:
我想在我的 ejs PDF 上显示关于使用 nodejs 的我的 MYSQL 数据 所以我在显示 PDF 格式的数据时遇到了问题: 这是我的节点 js 代码,我想将数据(MYSQL 数据库)从 JS 发送到 EJS 文件并以 pdf 格式显示:
const mysql = require('mysql')
pdf = require('express-pdf');
const ejs = require('ejs');
//Connection to database
const pool = mysql.createPool({
connectionLimit :10,
host :"localhost" ,
user :"root",
password :"",
database :"backend",
multipleStatements: true
})
module.exports={
//Creation Of pdf
pdfversion : function(req,res){
res.pdfFromHTML({
filename: 'templatee.ejs',
html: path.resolve(__dirname, './templatee.ejs')
});
},
//Getting the data from MYSQL database
getonetva : function(req , res){
pool.getConnection((err,connection)=>{
if(err) throw err
const id = req.params.id;
const data =`SELECT * FROM formulaire ` ;
connection.query(data, (err,rows) =>{
connection.release()
console.log(rows)
if(!err){
res.send(rows)
//res.render To send the data to EJS file , and i think i have the Problem here :
res.render('./templatee.ejs', {
results: JSON.stringify(rows)
});
console.log(rows);
}
else{
console.log(err)
}
})
} )
},
我们有 EJS 文件:
<div class="table-data">
<h2>Display Data Node.js & MySQL</h2>
<table border="1">
<tr>
<th> Name</th>
<th>Last Name </th>
<th>Email</th>
<th>Phone number</th>
</tr>
<tr>
<td><%=results.nom %></td>
<td><%=results.prenom %></td>
<td><%=results.email %></td>
<td><%=results.tel %></td>
</tr>
</table>
</div>
和路由器:
const fac = require('../Controller/gestclient');
const route = require('express').Router();
route.get('/topdf',fac.pdfversion);
route.get('/show',fac.getalltva);
module.exports = route
这是我输入 http://localhost:5000/gestion/topdf 时得到的结果:它显示了模板,但没有替换。
【问题讨论】: