 
     # Parse each Placemark into a mongoose model and save each
     _(result.Document.Folder.Placemark).each (placemark, i) ->
+        # Watch for the last camera since Model.save() is asynchronous and the
+        # database connection should be closed after the last successful save.
+        if i + 1 is result.Document.Folder.Placemark.length
+            isLast = true
+        else isLast = false
+
         url = URL.parse placemark.description.split('"')[3]
         coords = placemark.Point.coordinates.split ','
 

_ = require 'underscore'
URL = require 'url'
mongoose = require 'mongoose'

parser = new xml2js.Parser()



Error: #{err}"""

            if isLast
                CameraModel.count {}, (err, count) ->
                    console.log "Saved #{count} cameras successfully."
                    mongoose.disconnect()

fs.readFile 'data/raw/trafficcameraswithfeed.kml', (err, data) ->
    if err then console.log err else parser.parseString data
