Last active
August 29, 2015 13:57
-
-
Save 1dolinski/9588817 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() { | |
var locationSchema = mongoose.Schema({ | |
location: String, | |
latitude: Number, | |
longitude: Number | |
}) | |
var jobSchema = mongoose.Schema({ | |
job_id: String, | |
job_title: { | |
type: String, | |
required: true | |
}, | |
company: { | |
type: String, | |
required: true | |
}, | |
company_website: String, | |
headquarters: { | |
type: String, | |
required: true | |
}, | |
job_cateorgy: { | |
type: String, | |
required: true | |
}, | |
date_posted: Date, | |
}) | |
var Location = mongoose.model('Location', locationSchema) | |
var Job = mongoose.model('Job', jobSchema) | |
// Build out the Location collection | |
// check to see if the HQ exists, if it does then go to the next job | |
// if it doesn't add it to the Location collection | |
Job.find(function(err, jobs) { | |
if (err) return console.error(err); | |
jobs.forEach(function(job) { | |
Location.find({ | |
location: job.headquarters | |
}, function(err, data) { | |
if (data.length === 0) { | |
//////////////// This part loops, when writing it I thought it would wait 2500 or a quarter of a second | |
//////////////// for each job? | |
setTimeout(function() { | |
console.log("doesn't wait for timeout") | |
}, 2500) | |
// setTimeout(geocoder.geocode(job.headquarters, function(err, data) { | |
// if (err) { | |
// console.log("ERROR " + x.job_id) | |
// } else { | |
// console.log(data.results[0].geometry.location); | |
// } | |
// }), 12500) | |
} else { | |
} | |
}) | |
}) | |
}) | |
}) |
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() { | |
var locationSchema = mongoose.Schema({ | |
location: String, | |
latitude: Number, | |
longitude: Number | |
}) | |
var jobSchema = mongoose.Schema({ | |
job_id: String, | |
job_title: { | |
type: String, | |
required: true | |
}, | |
company: { | |
type: String, | |
required: true | |
}, | |
company_website: String, | |
headquarters: { | |
type: String, | |
required: true | |
}, | |
job_cateorgy: { | |
type: String, | |
required: true | |
}, | |
date_posted: Date, | |
}) | |
var Location = mongoose.model('Location', locationSchema) | |
var Job = mongoose.model('Job', jobSchema) | |
// Build out the Location collection | |
// check to see if the HQ exists, if it does then go to the next job | |
// if it doesn't add it to the Location collection | |
var locationq = function(hqs, jobs, i) { | |
Location.find({ | |
location: hqs | |
}, function(err, data) { | |
if (data.length === 0) { | |
console.log(hqs + " not found..") | |
geocode(jobs, hqs, i) | |
} else { | |
getJobs(jobs, i + 1) | |
} | |
}) | |
} | |
var geocode = function(jobs, location, i) { | |
geocoder.geocode(location, function(err, data) { | |
if (err) { | |
console.log("Geocoder is getting a little tired.. taking a quick break") | |
setTimeout(function() { | |
getJobs(jobs, i) | |
}, 2500); | |
} else { | |
data_posi = data.results[0].geometry.location | |
var location_data = new Location({ | |
location: location, | |
latitude: data_posi.lat, | |
longitude: data_posi.lng | |
}) | |
location_data.save(function(err, data) { | |
if (err) return console.error(err); | |
console.log("Saved location for " + data.location); | |
}); | |
setTimeout(function() { | |
getJobs(jobs, i + 1) | |
}, 2500); | |
} | |
}) | |
} | |
var getJobs = function(jobs, start) { | |
if (start >= jobs.length) { | |
console.log("DONE -- " + jobs.length + " have been looked after."); | |
return | |
}; | |
var i = start; | |
var hq = jobs[i].headquarters | |
locationq(hq, jobs, i) | |
} | |
Job.find(function(err, jobs) { | |
if (err) return console.error(err); | |
console.log("Getting jobs") | |
getJobs(jobs, 1); | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment