Skip to content

Instantly share code, notes, and snippets.

@heijmerikx
Last active October 15, 2017 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heijmerikx/b221e588496ded3a25d586eee627cb31 to your computer and use it in GitHub Desktop.
Save heijmerikx/b221e588496ded3a25d586eee627cb31 to your computer and use it in GitHub Desktop.
Refactor node.js snippet
function prepareDoc(item) {
var obj = {
id: item.id,
hash: item.hash,
title: item.title,
account_id: item.AccountId,
text: item.body,
createdAt: item.created_at,
updatedAt: item.updated_at,
attachments: prepareAttachments(item.Images),
};
return obj;
}
function prepareAttachments(attachments) {
var base64AttachmentArray = [];
for(var i=0;i < attachments.length;i++) {
var params = { Bucket: process.env.UPLOAD_BUCKET, Key: attachments[i].key };
s3.getObject(params, function(err, data) {
if (err) {
logger.error(err);
return err;
} else {
var objectData = {
data: data.Body.toString('base64')
};
base64AttachmentArray.push(objectData);
}
});
}
return base64AttachmentArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment