Function returning undefined Node JS

I am currently trying to iterate through an array of JSON elements, parse and add the data I need into a specially formatted string, and once conditions are met, initiate the uploading of this data.

The problem that I am running into, however, is my variable 'deviceOutString' is being returned as undefined, leaving me with a string of 'undefined' written as many time as there are JSON elements in the array. I know that the return from the 'checkDuplicates' function is correct because before returning the value, the logs show that the value is correct.

I have attached my code below, please let me know if you have any ideas.

Thanks!

Old Code (updated below)

    var i=0;
    var parsedJson = JSON.parse(storedData) ;
    var storedDataSize = parsedJson.length;
    console.log('Stored Data Size: '+storedDataSize);

    var async = require('async');

    async.each(parsedJson, function( subElemJson, callback1) {
        async.series([
            function(callback){
                console.log('dstring: ' + deviceOutString); 
                console.log('i : ' + i);

                var subElemJsonPretty = JSON.stringify(subElemJson,null,0) ;
                var date = subElemJson['date'];

                deviceOutString += checkDuplicates(subElemJson, deviceOutString);
                console.log('theLoop*DString: ' + deviceOutString);             

                callback(null, 'one');
            },

            function(callback){
                if((i == storedDataSize - 1 || count == 225) && storedDataSize > 0) {

                    writeDCC(deviceOutString);
                    count = 0;
                    makeList();
                }
                i++;                
                callback(null, 'two');
                setTimeout(function () { callback1(); }, 500);
            }
        ]);

    }); }

Updated New Code

function theLoop(storedData) {
var deviceOutString = '<list>';
var temp;   

try {
    var i=0;
    var parsedJson = JSON.parse(storedData) ;
    var storedDataSize = parsedJson.length;
    console.log('Stored Data Size: '+storedDataSize);   

    var async = require('async');

    var delayed = require('delayed');

    async.each(parsedJson, function( subElemJson, callback1) {
        async.series([
            function(callback){
                var subElemJsonPretty = JSON.stringify(subElemJson,null,0) ;
                var date = subElemJson.date;
                console.log('THIS IS THE DATE: '+date);

                temp = checkDuplicates(subElemJson, deviceOutString);
                console.log('This is the temp: ' + temp);

                callback(null, temp);
            }
        ],  function(results){
                console.log('*****Results are In*****: ' + results);
                deviceOutString =+ temp;

                if((i == storedDataSize - 1 || count == 225) && storedDataSize > 0) {

                    writeDCC(deviceOutString);
                    count = 0;
                    deviceOutString = '<list>';
                }
                i++;                
                callback1(results);
        });
        },              
        function(err){
            if( err ) {
              console.log('A file failed to process');
            } else {
              console.log('All files have been processed successfully');
            }
        });                 
} catch (error) {
    console.info('Exception parsing ' + '\n\n'  + error);
    return;
  } 
}

So a few things

1: var date = subElemJson['date']; accessing object properties via array syntax is a bad practice. Nit picky but hey :P try var data = subElemJson.date; instead.

2: deviceOutString isn't defined anywhere in the code you provided.

3: Both async.series and async.each are going to want a callback function for when each is finished. that's the whole point of calling callback(null, 'one'); -- that you pass a value to the "results" array in the final async.series callback. You are calling setTimeout(function() { callback1(); }, 500); in the wrong place (also arbitrarily putting it behind a timeout?).

The proper async.series formatting is thus:

async.series([
  function(callback) {
    // do stuff
    callback(null, someValue);
  },
  function(callback) {
    // do other stuff
    callback(null, someOtherValue);
  }
], function(results) {
  // all the stuffs are done
  console.log(results); <-- results is an array containing "someValue" and "someOtherValue" from the iterations above
  callback1(results);
});

Also, async.each is in the same boat -- it expects you to pass a "every thing I'm looping through has completed now!" function at the end.

Async docs on .each() (scroll down for docs on .series()): https://github.com/caolan/async#each