0

Here is my program as three modules.

App.js :

const geocode = require('./utils/geoCode')
const forecast = require('./utils/forecast')

geocode ('makkah', (error,data) =>{
    console.log('Error',error)
    console.log('Data',data)
    
})



forecast(23,76.0711, (error, data) => {
    console.log('Error', error)
    console.log('Data', data)
  }) '''

Forecast.js

const request = require('request')

const forecast = (latitude,longitude, callback) => {
     const url = 'http://api.weatherstack.com/current?access_key=e386783ec5de0478cb3b75c68d595f57&query=' + latitude + ',' + longitude +'&units=s'
     
     request ({url: url, json : true}, (error,response) =>{
            if (error){
                callback('Unable to connect to weather services', undefined)}

            else if (response.body.error){
                callback('The coordinates entered are wrong!',undefined)
            }
            else{

                const temperature =  response.body.current.temperature
                const feelslike = response.body.current.feelslike
                const description = response.body.current.weather_descriptions[0]
                const data = {
                    temperature : temperature,
                    feelslike : feelslike,
                    description : description,
                    string : description + '. The temperature is ' + temperature + '. It feel like ' + feelslike
                }
                callback(undefined, data.string)
            }
            }
     
         )}

        
module.exports = forecast

geocode.js :

const request = require('request')

const geocode = (address, callback) => {
    const url1 ='https://api.mapbox.com/geocoding/v5/mapbox.places/' + encodeURIComponent(address) + '.json?access_token=pk.eyJ1IjoiaGFkaWsxMjMiLCJhIjoiY2t6NWNkNjR5MGtkdDJ2cWZkNHpveGQwNSJ9.eP8UIvbVL0R3Bg-guZbAnw&limit=1'
    

    request ({ url:url1, json:true}, (error,response) =>{
        if (error){
            callback(error,undefined)
        } else if (response.body.features.length == 0){
            callback('The input is not correct!',undefined)
        } else{
            const longitude =response.body.features[0].center[0] 
            const latitude =response.body.features[0].center[1]
            const location =response.body.features[0].place_name
            const data = {
                longitude :longitude,
                latitude : latitude,
                location : location
            }
            callback(undefined,data)
        }

    })
}

module.exports = geocode

OUTPUT IS THIS :

Error undefined
Data Sunny. The temperature is 301. It feel like 299
Error Error: connect ETIMEDOUT 64:ff9b::1243:96ba:443
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) {
  errno: -4039,
  code: 'ETIMEDOUT',
  syscall: 'connect',
  address: '64:ff9b::1243:96ba',
  port: 443
}
Data undefined

As you can see, npm request in geocode module is showing error, while same npm in forecast modules is working fine .

I am running on VScode and have installed latest version of request@2.88.2

The program was working correctly till yesterday night and throws error now.

1 Answers1

0

Half a day gone , and it got solved by a simple semi colon in the first line of geocode.

const request = require('request');