title: mongoose findone callback scope (NodeJS, express)
body: <p>I'm trying to scrape data from an external JSON feed and store it in my mongoDB. </p>
  
  <pre><code>request(url, function (error, response, body) {
      if (!error &amp;&amp; response.statusCode == 200) {
          var jsonObj = JSON.parse(body);
          // console.log(jsonObj.events[1].id) &lt;-- this works
          for (var i = 0; i &lt; jsonObj.events.length; i++) { 
              // add jsonObj.events[i] as a new record to table
              // console.log(jsonObj.events[i].id) &lt;-- this works
              Wnet.findOne({ id : jsonObj.events[i].id }, function (err, doc){
                  if (err || doc == null) {
                       // console.log(jsonObj.events[i].id) &lt;-- this doesn't work!
                       // record is new, add it
                  }
              });
           }
       }
  });
  </code></pre>
  
  <p>Notice the <code>console.log</code> statements... the last one (inside the <code>.findOne</code>'s callback) doesn't work. </p>
  
  <p>I'm still learning the ins and outs of JavaScript, but I believe this is a scope issue... the callback function doesn't know jsonObj. But, I don't know how to pass it into the function.</p>
  
  <p>Taking a step back, I'm trying to check the collection for existence of the record. If it can't find it, I want to add the record from the fetched JSON array. If there's a better way to do that, I'd love your help in finding it.</p>