-1

I am trying to find the three midpoints for three different locations using the polygon with getMidPoint function .while trying to return the array in the getMidPoint function. The array values are not assigning to midPoints variable, I'm getting Cannot read property 'length' of undefined as an error in terminal

var midPoints = getMidPoint(Location1, Location2)
    // Directions API
    var i = 0;
    var array = [];
    function getMidPoint(a, b) {
        console.log("I am working...")
        clientMap.post(config_KEYS.DIRECTION_API + a + "&destination=" + b + "&key=" + config_KEYS.GOOGLE_API_KEY + "&departure_time=" + timeStamp + "", function (googleDirectionData, error) {
            if (googleDirectionData.status == "ZERO_RESULTS" | googleDirectionData.status == "INVALID_REQUEST") {
                res.status(400).send(googleDirectionData)
            }
            else {
                var polyline = googleDirectionData.routes[0].overview_polyline.points
                var decoded = decode(polyline)                
                var n = Math.round(decoded.length / 2)
                var obj = {}
                obj.latitude = decoded[n].latitude
                obj.longitude = decoded[n].longitude
                array.push(obj)   
            }  
        })
        return array    
    }

    if (midPoints && midPoints.length == 1) {
        console.log("durationpointDat   dfggdfg"+ midPoints.length)
        var durationpointData2 = getMidPoint(Location2, Location3)
    }
    else {
        if (midPoints.length == 2) {
            console.log("durationpointData fdf"+ midPoints.length)
            var durationpointData3 = getMidPoint(Location3, Location1)                        
        }
        else{
                res.status(200).json(array)
        }
    }

Suggestions will be appreciated.

RAHUL SRV
  • 262
  • 3
  • 24

2 Answers2

1

As i can seen in your code in getMidPoint you are calling google map API. And it is asynchronous call means before completing the Google API call your code is executing next code so in midPoints you are getting undefined.

Means you are returning the array before completing the google map api. you have to create a callback and pass data into it.

you can follow this example.

var midPoints;
getMidPoint(Location1, Location2, function(error,data){
    // You can check for error here
    midPoints = data;
})
// Directions API
var i = 0;
var array = [];
function getMidPoint(a, b, callback) {
    console.log("I am working...")
    clientMap.post(config_KEYS.DIRECTION_API + a + "&destination=" + b + "&key=" + config_KEYS.GOOGLE_API_KEY + "&departure_time=" + timeStamp + "", 
        function (googleDirectionData, error) {
        if (googleDirectionData.status == "ZERO_RESULTS" | googleDirectionData.status == "INVALID_REQUEST") {
            res.status(400).send(googleDirectionData)
        } else {
            var polyline = googleDirectionData.routes[0].overview_polyline.points
            var decoded = decode(polyline)                
            var n = Math.round(decoded.length / 2)
            var obj = {}
            obj.latitude = decoded[n].latitude
            obj.longitude = decoded[n].longitude
            array.push(obj)
            callback(null, array);
        }  
    }) 
}
Aabid
  • 953
  • 5
  • 23
0

Try to declare var array in a function and return it. Not in a global scope

Efkiss
  • 95
  • 3
  • 10
  • yes,I assinged the array in the function but the return array statement value is not assigned to midPoint and the midpoint length is undefined – RAHUL SRV Jul 27 '18 at 11:29