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.