Why is it when I save my API key, location, etc. in a constant the app crashes? If I comment the first const url
and I leave the params inside the url works just fine.
app.get('/', function(req, res) {
const query = "London";
const apiKey = "c3fd0e578a1e8ead729deb6f25";
const unit = "metric";
const url = "https://api.openweathermap.org/data/2.5/weather?q=" + query + "&appid=" + apiKey + "&units=" + unit;
const url = "https://api.openweathermap.org/data/2.5/weather?q=London&appid=c3fd0e578a1e8ead729deb6f25d&units=metric"
https.get(url, function(response) {
console.log(response.statusCode);
response.on("data", function(data) {
const weatherData = JSON.parse(data);
const temp = weatherData.main.temp;
const weatherDescription = weatherData.weather[0].description;
const icon = weatherData.weather[0].icon;
const iconURL = "http://openweathermap.org/img/wn/" + icon + "@2x.png";
res.set("Content-Type", "text/html");
res.write("<h1>The temperature in London is " + temp + "C</h1>");
res.write("<p>The weather is currencly " + weatherDescription + "</p>");
res.write("<img src=" + iconURL + ">");
res.send();
});
Here is what I get in the Terminal. It points me to the const temp
but there is nothing wrong with it, if I comment it out, then it points me to the next one.
/Users/Desktop/code/Web Dev. Bootcamp/WeatherProject/app.js:22
const temp = weatherData.main.temp;
^
TypeError: Cannot read properties of undefined (reading 'temp')
at IncomingMessage.<anonymous> (/Users/Desktop/code/Web Dev. Bootcamp/WeatherProject/app.js:22:43)
at IncomingMessage.emit (node:events:513:28)
at Readable.read (node:internal/streams/readable:539:10)
at flow (node:internal/streams/readable:1023:34)
at resume_ (node:internal/streams/readable:1004:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
Node.js v18.13.0
[nodemon] app crashed - waiting for file changes before starting...