I am currently building a ridesharing app on iOS and have run into some issues while using the Google Distance Matrix in my Node.js code.
Below is the code:
distance.matrix(leg3_origin, leg3_destination, function (err, distances) {
console.log('distance');
if (err) {
console.log(err);
return console.log(err);
}
else if(!distances) {
console.log('no distances..');
return console.log('no distances');
}
else{
console.log('if statement');
var origin = distances.origin_addresses[0];
var destination = distances.destination_addresses[0];
leg3Duration = distances.rows[0].elements[0].duration;
leg3Distance = distances.rows[0].elements[0].distance;
console.log(leg3Distance);
if (leg3Distance == null)
{
leg3Distance = 0;
}
console.log('entering 2nd leg');
getSecondLeg(driverArrivalTime2, leg3Duration, leg3Distance);
}
});
The line leg3Duration = distances.rows[0].elements[0].duration;
is causing the error when trying to call .elements.
I understand it's breaking because leg3Duration
is undefined, but it is being initialized in the function. Is there any other way I should be initializing this value to avoid the error?
Thank you for help in advance!