With the following code being asynchronous, it is my belief that the lifespan of the 'recipeData' array is not long enough to outlast the asynchronous callbacks, so I made a copy of the data in a global array, but I am still receiving the same error "TypeError: Cannot read property '_id' of undefined". Here is the code:
var tempArray;
function getAllRecipes( db, callback )
{
query( db, {}, 'recipes', function( err, recipeData )
{
tempArray = recipeData.slice();
if( err || !tempArray.length )
callback( err );
else
{
var taskArr = [];
for( var i=0; i < tempArray.length; i++ )
{
taskArr.push( function( iCallback )
{
getRecipeNotes( db, tempArray[ i ]._id, function( err2, noteData )
{
if( err2 )
iCallback( err2 );
tempArray[ i ].notes = noteData;
iCallback();
});
});
}
taskArr.push( function( err3 )
{
callback( err3, tempArray );
});
async.parallel( taskArr );
}
});
}
The error is occurring on the call to 'getRecipeNotes'.
Can someone please help me work out where I am going wrong?
Thank you.