0

Why when I run the npm request module the output I get is undefined?

const request = require('request');
request({
  url: 'https://maps.googleapis.com/maps/api/geocode/json?address=1301%20lombard%20street%20philadelphia',
  json: true
  }, (error, response, body) => {
     console.log(body);
});
Yahya
  • 706
  • 8
  • 23

2 Answers2

0

Seems like your code should work properly if request is installed. The possible reason why you get undefined is a problem with your internet connection.

Start with checking if you can access Internet at all. You can try this code:

 require('dns').resolve('www.google.com', function(err) { 
   if (err) { 
     console.log("No connection"); 
   } else { 
     console.log("Connected"); 
     } 
 });
Teukros
  • 82
  • 5
  • I have installed request.What might be the problem with internet connection? – sudo_shreyas Dec 08 '17 at 08:27
  • You just don't have internet connection from your app. Maybe it's blocked by the firewall. Your code works well on my machine, but I also receive 'undefined' if I switch off WiFi. – Teukros Dec 08 '17 at 08:49
  • Then what am i supposed to do?? – sudo_shreyas Dec 08 '17 at 08:58
  • I would start with checking if that's the reason. You can try it with this code and see if your app is able to connect with the Internet: `require('dns').resolve('www.google.com', function(err) { if (err) { console.log("No connection"); } else { console.log("Connected"); } });` Unfortunately, there's plenty of reasons why you can't access internet. – Teukros Dec 08 '17 at 09:29
  • The output is 'connected'. – sudo_shreyas Dec 08 '17 at 12:35
  • And what you receive if you console log everything? error and response? As I said, your code works fine on my machine. – Teukros Dec 08 '17 at 12:50
  • This is something I get error: { Error: connect EINVAL 0.0.0.12:8080 - Local (0.0.0.0:0) at Object._errnoException (util.js:1024:11) at _exceptionWithHostPort (util.js:1046:20) at internalConnect (net.js:971:16) at GetAddrInfoReqWrap.emitLookup [as callback] (net.js:1106:7) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:97:10) code: 'EINVAL', errno: 'EINVAL', syscall: 'connect', address: '0.0.0.12', port: 8080 } statusCode: undefined body: undefined – sudo_shreyas Dec 08 '17 at 12:52
0

I checked for proxy using env|grep -i proxy and then unset proxy for http_proxy or other proxies which is set.The command for that is:unset http_proxy