我在node.js/express、ejs和postgresql中工作。
我正在试着插入 <script></script>
标记从请求中获得的ejs代码。
试着这样做,但没用:
<% avg_t.rows.forEach(function(avg){%>
<script type="text/javascript">
var first = JSON.stringify('<%= avg.mark %>');
console.log(first);
</script>
<%} ) %>
执行请求的node.js代码:
const graphic1 = (req,res)=> {
const db = new pg.Pool(te_role.client)
db.connect(function(err, client, done){
console.log("")
if(err){
return console.error('canceled')
}
client.query(teacher_role.show_avg,[log2,req.body.disce,req.body.numb,req.body.lett], function(err, result){
return_data.avg_t =result
if (err){
res.end()
return console.error("canceled")
}
done()
res.render('inside',{avg_t:return_data.avg_t})
})
})
}
app.get('/inside',urlencodedParser,db_te.get_graphic1)
当然,我的完整ejs代码,我尝试:
<% avg_t.rows.forEach(function(avg){%>
<script type="text/javascript">
var first = JSON.stringify('<%= avg.mark %>');
console.log(first);
google.charts.load('current', {
'packages': ['corechart']
});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
['Mushrooms', first],
['Onions', 1],
]);
var barchart_options = {
title: 'Barchart: How Much Pizza I Ate Last
Night ',width:400,height:300,legend: '
none '};
var barchart = new google.visualization.BarChart(document.getElementById('barchart_div'));
barchart.draw(data, barchart_options);
}
</script>
<%} ) %>
暂无答案!
目前还没有任何答案,快来回答吧!