Last active
October 15, 2017 13:15
-
-
Save heijmerikx/b221e588496ded3a25d586eee627cb31 to your computer and use it in GitHub Desktop.
Refactor node.js snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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