-1

I need to retrieve the coordinate that the request sends me back.

The request is correct, on body body I can access lat and lon. The thing is, the request is inside another function called latlongfunc. How can I get access to the body outside the request call?

What I've already tried: create a variable before the call, then, modify it inside the call, and finally return it on the end of the function latlongfunc. It doesn't work...

IMPORTANT: The request IS working, the problem is on how to access the body outside the request.

const request = require('request')
console.log("Here.")


var latlongfunc = async (fullAddress) => {

  var options = {
    url: `https://nominatim.openstreetmap.org/search/${fullAddress}`,
    json: true, // JSON strigifies the body automatically
    headers: {
      'User-Agent': 'request'
    }
  };

  request(options, (err, res, body) => {
    if(body.length > 0){
      // A body was received
      var coordinate = {
        lat: body[0].lat,
        lon: body[0].lon
      }

      return coordinate
    }else{
      console.log("Something wrong happened")

    }
  })


}

module.exports = {
  latlongfunc
};
Zowye
  • 185
  • 1
  • 2
  • 12

2 Answers2

0

just wrap your code in a promise that resolves with the coordinate you need.

const request = require('request')
console.log("Here.")


var latlongfunc = (fullAddress) => {

  var options = {
    url: `https://nominatim.openstreetmap.org/search/${fullAddress}`,
    json: true, // JSON strigifies the body automatically
    headers: {
      'User-Agent': 'request'
    }
  };

  return new Promise((resolve, reject) => {
    request(options, (err, res, body) => {
      if(err) {
        return reject(err);
      }

      if(body.length > 0){
        // A body was received
        var coordinate = {
          lat: body[0].lat,
          lon: body[0].lon
        }

        resolve(coordinate);
      }else{
        console.log("Something wrong happened")

      }
    })
  })

}

module.exports = {
  latlongfunc
};


const latlongfunc = require("latlongfunc");
latlongfunc.then(coordinate => console.log(coordinate));
Karim
  • 8,454
  • 3
  • 25
  • 33
  • How do I deal with errors? Actually I was using `Promises` but I was trying to get rid of this (I thought it was kind of deprecated). In case of an unsuccessful request, how I deal with? – Zowye Apr 10 '19 at 17:20
  • you reject the promise with the error and then you handle the exception in the caller. i've fixed the code – Karim Apr 10 '19 at 17:25
0

My suggestion is to use the request-promise package which wraps request with Promise

const rp = require('request-promise')

const request = require('request')

var latlongfunc = async (fullAddress) => {
  let coordinate
  try {
    var options = {
    url: `https://nominatim.openstreetmap.org/search/${fullAddress}`,
    json: true, // JSON strigifies the body automatically
    headers: {
      'User-Agent': 'request'
    }
  };

  const body = await rp(options)
  var coordinate = {
    lat: body[0].lat,
    lon: body[0].lon
  } catch (e) {
    // handle error or throw
    console.log("Something wrong happened")
  }
  return coordinate
}
1565986223
  • 6,420
  • 2
  • 20
  • 33