Here I am using this function to check the DB if the place already exists in the database, if not I plan to add it (not yet implemented).
function pairPlaces(parsed, callback) {
for (var placeKey in parsed.results) {
var place = parsed.results[placeKey];
var placeid = place.place_id;
var name = place.name;
var lat = place.geometry.location.lat;
var lng = place.geometry.location.lng;
var vicinity = place.vicinity;
console.log(placeKey + "::" + name + "-" + placeid);
client.execute("select * from places where placeid='"+placeid+"'", function(err, result) {
if(!err) {
if(result.rows.length > 0) {
console.log('Found...');
} else {
console.log('Added: ' + name);
}
}else {
console.log('DB ERR: %s', err);
}
});
}
callback(parsed);
}
However, after it checks the database and sees that none of the places are added the console logs the same place name as the last one in the loop as opposed to the corresponding place that would have been queried. What am I doing wrong?
Console output from the above function:
0::Harry's Chocolate Shop-ChIJNx4C967iEogRjyKJNWYeSjs
1::Brother's Bar & Grill-ChIJV5TTw67iEogRZF2mm1f6K-0
2::Jake's Roadhouse-ChIJ6SGX2a7iEogRB_d9evKamGQ
3::Qdoba Mexican Grill-ChIJofbRu67iEogRpPnhaYGiwl0
4::Juilliard-ChIJSUzXka7iEogRlPiKfmP5fc4
Added: Juilliard
Added: Juilliard
Added: Juilliard
Added: Juilliard
Added: Juilliard