I am trying to build a webapp which renders dynamic charts based on JSON passed from node to client side.
However I am not able to access json variable inside <script>
. I have tested JSON object using
console.log(<%= data %>)
it is appearing there.
My Node code looks like:
let ltlasJSON = JSON.parse(ltlasData);
app.get("/results", function(req, res){
var query = req.query.postalCode;
var url = "http://api.postcodes.io/postcodes/" + query;
request(url, function(error, response, body){
if(!error && response.statusCode == 200){
var data = JSON.parse(body);
var adminDistrict = data["result"]["codes"]["admin_district"];
var filtered = ltlasJSON.filter(a=>a.areaCode==adminDistrict);
res.render("results", {data: filtered});
}
}
);
});
results.ejs:
<<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Engliand Covid-19 </title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="style.css">
<!-- Plotly.js -->
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<!--[if lt IE 7]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<h1>Results Page</h1>
<script>
var specimenDate = <%= data.areaCode %>
var cases = <%= data.dailyLabConfirmedCases %>
var data1 = [
{
x: specimenDate,
y: cases,
type: 'bar'
}
];
Plotly.newPlot('myDiv', data);
</script>
</body>
</html>
I am new to node and ejs. End goal is to pass array of JSON (two fields) in x and y variable of plotly object.