This is my JS code from my router, I'm running a Node Express server with Pug.
var express = require('express');
var router = express.Router();
var https = require('https');
/* GET home page. */
router.get('/', function(req, res, next) {
var url = 'https://data.police.uk/api/forces';
var data = [];
https.get(url, function(res){
var body = '';
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
var response = JSON.parse(body)
for (var i = 0; i< response.length; i++)
for (var id in response[i]) {
data[i] = response[i]
}
console.log(data[0].id);
});
}).on('error', function(e){
console.log("Error: ", e);
})
res.render('map', {
title: 'data[0].id'
});
});
module.exports = router;
And this is my Pug file to be rendered
p Welcome to #{title}
The console.log in 'res.on()' works perfectly fine and shows the JSON package I was expecting. When I call this array in res.render() i.e. title: 'data[0].name' it throws an undefined error.