My geocode.js class is below.
const request= require('postman-request')
async function geocode (address, callback){
const url= `https://api.mapbox.com/geocoding/v5/mapbox.places/${encodeURIComponent(address)}.json?access_token=<myToken>&language=tr&limit=1`
request({url, json:true},(error,{body})=>{
if (error) {
callback('Unable to connect to location services!',undefined)
}else if (body.features.length===0) {
callback('Unable to find the location!',undefined)
} else {
callback(undefined,{
latitude: body.features[0].center[1],
longitude: body.features[0].center[0],
location: body.features[0].place_name
})
}
})
}
module.exports= geocode;
My app.js is below.
"use strict";
const geocode = require('../src/geocode.js');
var addressArray= ["Address1", "Address2", "Address3"];
var encodingAddressArray = [];
function getGeoCode(e){
geocode(e,(error,{latitude,longitude, location}={})=>{
// Here the latitude, longitude and location fields come full
return {
latitude: latitude,
longitude: longitude,
location: location
}
})
}
addressArray.forEach(e => {
// But, when I call the getGeoCode function here, it returns undefined.
encodingAddressArray.push(getGeoCode(e))
});
console.log(encodingAddressArray)
The geocode.js class is called within the app.js class. My aim is to print the response data from the request that I have made with the mapbox Api in geocode.js into an array with the forEach loop in app.js. However, when I want to print the response I called from geocode.js to the array I defined in app.js, the data comes as undefined. What should I do? Thank you.