NVO NVO -4 years ago 118
Node.js Question

async does not have whole object in iteration

I use async.eachSeries to iterate over an array. The array looks like:

{ filename: '20171-132.pdf',
content: [base64 encoded file]
}


With a loop I loop through the items to get the contents.

async.eachSeries(attach, function(item, callback){
console.log("ITEM", item);
var attachment = {
filename: item.filename,
content: item.content.split("base64")[1],
encoding: 'base64'
}

attachments.push(attachment);
callback();
}, function(){
console.log("done");
})


But, in the log of 'item', I have only filename, with as result, that the code stops on item.content.split, because it's undefined.
Anybody any idea whats happening here and how can I solve this?

Answer Source

You are working on an object, not an array.

{ 
   filename: '20171-132.pdf',
   content: [base64 encoded file]
}

Square brackets are the JSON representation of an array. So if item would look like this instead;

[
    { 
       filename: '20171-132.pdf',
       content: [base64 encoded file]
    }
]

then I'm guessing it will fix your problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download