我正在使用快速js来渲染包含一些数据的页面。我发送给客户端的响应中包含多个对象。我写的代码如下 -
app.js
var express = require('express');
var path = require('path');
var app = express();
var port = process.env.PORT || 5000;
var delivCount = require('./serv_delivered.js');
var serv_ontime = require('./serv_ontime.js');
var delayedCount = require('./serv_delayed.js');
var crtidelayedCount = require('./serv_crit_delayed.js');
var chartData = [{
"country": "Czech",
"litres": 301.9
}, {
"country": "Ireland",
"litres": 201.1
}, {
"country": "Germany",
"litres": 165.8
}, {
"country": "Pak",
"litres": 139.9
}, {
"country": "Austria",
"litres": 128.3
}, {
"country": "UK",
"litres": 99
}] ;
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', __dirname + '/src/views');
var bookRouter= express.Router();
app.set('view engine','ejs');
app.get('/',function(req,res){
delivCount(function(error, res_deliv){
serv_ontime(function(error, res_ontime){
delayedCount(function(error, res_delayed){
crtidelayedCount(function(error, res_cdelayed){
/* res.render('index', {title: "Hello From Render",
res: {res_deliv,res_ontime,res_delayed,res_cdelayed,chartData}
}); */
//console.log(result);
res.send({res_deliv,res_ontime,res_delayed,res_cdelayed,chartData});
});
});
});
});
});
app.listen(port,function(err){
console.log('running server on port'+ port);
});
index.ejs
<html>
<head>
<title>
</title>
</head>
<body>
<p>Count 1 = <%= res.res_deliv.CNT_DELIV %> </p>
<p>Count 1 = <%= res.res_ontime.CNT_ONTIME%> </p>
<p>Count 1 = <%= res.res_delayed.CNT_DELAYED%> </p>
<p>Count 1 = <%= res.res_cdelayed.CNT_CDELAYED%> </p>
<p>Chart data: =<%res.chartData%> </p>
</body>
</html>
现在,当我只使用 res.send 时,我在localhost上获得的输出就是这样 -
此外,当我使用 res.render 并传递相同内容并尝试在客户端使用这些值时,就像在 index.ejs 中一样,我得到&# 39;未定义&#39;在所有这些地方,数据没有显示出来。任何人都可以帮我在客户端使用这些数据吗?