Last active
August 29, 2015 13:57
-
-
Save 1dolinski/9635199 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
mongoose.connect('mongodb://localhost/test'); | |
var db = mongoose.connection; | |
var geocoder = require("geocoder"); | |
var db = mongoose.connection; | |
db.on('error', console.error.bind(console, 'connection error: Make sure thatmongod is running')); | |
db.once('open', function callback() { | |
Location = require('./schema/location.js')() | |
Job = require('./schema/job.js')() | |
Message = require('./message.js') | |
Job.find(function(err, jobs) { | |
if (err) return console.error(err); | |
var jobs = jobs; | |
Message.start(); | |
var getJobs = function(i) { | |
if (i >= jobs.length) { | |
Message.finish(jobs.length); | |
return | |
}; | |
var hq = jobs[i].headquarters | |
locationq(hq, i) // check location | |
} | |
var locationq = function(hqs, i) { | |
Location.find({ | |
location: hqs | |
}, function(err, data) { | |
if (data.length === 0) { | |
Message.location_not_found(hqs) | |
geocode(hqs, i) // hq not found, run the geocoder to find it's coordinates and add it | |
} else { | |
getJobs(i + 1) // hq was found, move to the next | |
} | |
}) | |
} | |
var geocode = function(location, i) { | |
geocoder.geocode(location, function(err, data) { | |
if (err) { | |
setTimeout(function() { | |
// Further error reporting could occur here | |
Message.geocode_error | |
getJobs(i) | |
}, 2500); | |
} else { | |
data_posi = data.results[0].geometry.location // Location was found. | |
var location_data = new Location({ | |
location: location, | |
latitude: data_posi.lat, | |
longitude: data_posi.lng | |
}) | |
location_data.save(function(err, data) { | |
// Save location in | |
if (err) return console.error(err); | |
Message.location_saved(data.location) | |
}); | |
setTimeout(function() { | |
getJobs(i + 1) // | |
}, 2500); | |
} | |
}) | |
} | |
getJobs(0); | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
mongoose.connect('mongodb://localhost/test'); | |
var db = mongoose.connection; | |
var geocoder = require("geocoder"); | |
var db = mongoose.connection; | |
db.on('error', console.error.bind(console, 'connection error: Make sure thatmongod is running')); | |
db.once('open', function callback() { | |
Location = require('./schema/location.js')() | |
Job = require('./schema/job.js')() | |
Message = require('./message.js') | |
Job.find(function(err, jobs) { | |
if (err) return console.error(err); | |
Message.start(); | |
getJobs(jobs, 0); | |
}) | |
var getJobs = function(jobs, i) { | |
if (i >= jobs.length) { | |
Message.finish(jobs.length); | |
return | |
}; | |
var hq = jobs[i].headquarters | |
locationq(hq, jobs, i) // check location | |
// passing jobs from the original function into the new one, is this normal practice? | |
} | |
var locationq = function(hqs, jobs, i) { | |
Location.find({ | |
location: hqs | |
}, function(err, data) { | |
if (data.length === 0) { | |
Message.location_not_found(hqs) | |
geocode(jobs, hqs, i) // hq not found, run the geocoder to find it's coordinates and add it | |
} else { | |
getJobs(jobs, i + 1) // hq was found, move to the next | |
} | |
}) | |
} | |
var geocode = function(jobs, location, i) { | |
geocoder.geocode(location, function(err, data) { | |
if (err) { | |
setTimeout(function() { | |
// Further error reporting could occur here. The | |
Message.geocode_error | |
getJobs(jobs, i) | |
}, 2500); | |
} else { | |
data_posi = data.results[0].geometry.location // Location was found. | |
var location_data = new Location({ | |
location: location, | |
latitude: data_posi.lat, | |
longitude: data_posi.lng | |
}) | |
location_data.save(function(err, data) { | |
// Save location in | |
if (err) return console.error(err); | |
Message.location_saved(data.location) | |
}); | |
setTimeout(function() { | |
getJobs(jobs, i + 1) // | |
}, 2500); | |
} | |
}) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment