0

I have a pretty simple node script which parses a json file from an external url. I'm attempting to have the script loop over each record returned and make a decision to add it to the DB (using nedb) if we haven't previously got it. At the moment my script below looks to be only processing the last record in the json file.

var Datastore = require('nedb')
  , db = new Datastore({ filename: 'foo.db', autoload: true });


var request = require('request');
;

var cgrecentsalesurl = "http://ffo.com.json";

request({
    url: cgrecentsalesurl,
    headers: {
        'User-Agent': 'cgapistats'
    },
    json: true
}, function (error, response, body) {

    if (!error && response.statusCode === 200) {

        var cgrecentsales = body["recent-sales"];

        for (var i in cgrecentsales) {
            console.log( "processing record " + i );
            (function() {
            var query = { saletimestamp : cgrecentsales[i].sold_at };
            db.find( query , function( err, docs ) {
                 console.log( docs.length );
                 if ( docs.length == 0 ) {
                    db.insert( { item: cgrecentsales[i].item, saletimestamp: cgrecentsales[i].sold_at, amount: cgrecentsales[i].amount } );
                    console.log( "db record inserted" );
                 } else {
                    console.log( "record exists!" )
                 };
            });
            })();
        }
    } else {
        console.log(response);
    }
})

Any ideas what I'm doing wrong? Thanks!

Colm Troy
  • 1,947
  • 3
  • 22
  • 35
  • 4
    Infamous loop issue, you have the IIFE, but you're not closing in `i`, do it like this -> **http://jsfiddle.net/z4xcL36L/** – adeneo Mar 08 '15 at 20:36
  • @adeneo you're a legend! thanks so much for the quick solution - works perfectly now :) – Colm Troy Mar 08 '15 at 21:17

0 Answers0