Sunday 18 March 2018

Module somehow not receiving variable

Hi, I have this weather forecast app where the weather is retrieved based on the users location.However, there is this strange behaviour where if I pass in my hard coded ip address to the line where.is('87.92.111.226', function(err, result) { I get the correct result, but if I pass the variable ipAddress, the node-where module fails to find the location details. The strange part is, if I res.send(ipAddress); the ipAddress just before that line, it outputs my ip address, so the ip address should be passed to that line. How come it's failing? Thanks.const express = require('express'); const app = express(); const request = require('request'); const where = require('node-where'); // Callback that ends the process *when* the response is sent const endSuccess = () => process.exit(0); const apiKey = '********************'; let ipAddress; let city; let weatherForcast; let url; app.get('/!/node-weather', function(req, res) { ipAddress = req.connection.remoteAddress; if (ipAddress.substr(0, 7) == "::ffff:") { ipAddress = ipAddress.substr(7) } if (ipAddress) { // Call node-where module method to get location details from ip address //res.send(ipAddress); where.is(ipAddress, function(err, result) { if (result) { //res.send(result); if(result.get('city')){ city = result.get('city'); } else { res.send("City not found"); } url = `http://ift.tt/2pmJPiR; // Call request module method to query the api request(url, function (err, response, body) { if(err){ res.send('Error:', error); } else { let weather = JSON.parse(body); weatherForcast = `It's ${weather.main.temp} degrees in ${weather.name}!`; res.send("Done: " + weatherForcast); } }); } else { res.send(err); } }); } else { res.send("No IP Address found."); } if (res.headersSent) { res.end('Done', endSuccess); } }) app.listen(parseInt(process.env._2038_PORT), function(e) { if (e) { console.error(e); process.exit(1); } console.log(process.env._2038_READY); });

Submitted March 18, 2018 at 07:16PM by hey__its__me__

No comments:

Post a Comment