javascriptnode.jsfunctiongeocodegeocoder

How to use geocode function more than once in the same post request (npm: node-geocoder)?


I have been trying to create a function returnCoords(par1) on which the user can enter a text input, and convert it to coordinates using the node-geocoder npm. Although the function looks to work okay, when returning the function it gives undefined results. I tried fixing this with async and await but nothing seems to work, either with or without these.

I have also tried passing an object/array (of two texts) in geocoder.geocode(array/object, async function(err, data){...}) but also doesn't seem to work as it only accepts String values.

var express = require('express');
var router = express.Router();
var nodeGeocoder = require('node-geocoder');

var geocoderOptions = {
    provider: "google",
    httpAdapter: "https",
    apiKey: process.env.GEOCODER_API_KEY, //collect key from hidden environment variable
    formatter: null
}

var geocoder = nodeGeocoder(geocoderOptions);

router.post('/updateMap', async (req, res) => {
    //GET USER TEXT (MAP) INPUT & convert to coordinates  
    //req.body.mapFrom: "LA, USA"  
    var from_coords = await returnCoords(req.body.mapFrom);

    console.log("TEST 1:" + typeof(from_coords));
    console.log("TEST 2:" + from_coords);
    //console.log("TEST 3:" + from_coords[0]);
    //console.log("TEST 4:" + from_coords[0].latitude);
    //console.log("TEST 5:" + from_coords[0].longitude);
    
});

async function returnCoords(userInput){
    geocoder.geocode(userInput, async function(err, data){

        if(err || !data.length) {
            req.flash('error', 'Invalid address');
            return res.redirect('back');
        }
        
        console.log("LAT:" + data[0].latitude);
        console.log("LNG:" + data[0].longitude);
        
        return data;
    });
}

CONSOLE LOG prints in this order:

TEST 1 : undefined
TEST 2 : undefined
LAT    : 34.0522342
LNG    : -118.2436849

SOLUTION:

router.post('/updateMap', async (req, res) => {
    //GET USER TEXT (MAP) INPUT & convert to coordinates    
    let from_coords = await geocoder.geocode(req.body.mapFrom);
    let to_coords = await geocoder.geocode(req.body.mapTo);
    var method = req.body.mapMethod; //Radio Button Input

    console.log("FROM   : " + from_coords[0].longitude +" , "+from_coords[0].latitude);
    console.log("TO     : " + to_coords[0].longitude +" , "+to_coords[0].latitude);
    console.log("METHOD : " + method);
});
FROM   : -118.2436849 , 34.0522342 //(LA, USA)
TO     : -119.4179324 , 36.778261  //(CA, USA)
METHOD : walk

Solution

  • returnCoords is invalid giving your intent (you don't want to "fork" geocoding function), but return Promise that resolves with geocode result.

    Note that geocoder.geocode supports Promise-based API*, so for it should be enough to just change returnCoords to:

    async function returnCoords(userInput) {
        const data = await geocoder.geocode(userInput);
        ... // logs and stuff
        return data
    }