Open fr4style opened 9 years ago
The addFile method returns a Promise. You need to wait for it to be completely executed.
email.addFile('filename', file).then(function(e) {
console.log(e);
});
Hi elbuo8, thank you for the precious support. I changed my code using a recursive method that attaches all files of the array and sends the email when the array ends:
function attachToEmailAndSend(email, index, attachments){
email.addFile("Allegato_"+(index+1), attachments[index])
.then(function(e) {
index++;
console.log(" -- index: "+index+", size: "+attachments.length);
if(index < attachments.length){
attachToEmailAndSend(email, index, attachments);
}else{
sendgrid.sendEmail(email)
.then(function(httpResponse) {
console.log(httpResponse);
},function(httpResponse) {
console.error(httpResponse);
});
console.log(" -- Email Sent");
}
});
}
Unfortunately I incurred in this error:
Result: URIError: URI malformed
at encodeURIComponent (native)
at <anonymous>:325:38
at Parse.js:1:20710
at Array.forEach (native)
at Function.x.each.x.forEach (Parse.js:1:661)
at b._objectEach.b._each (Parse.js:1:20683)
at formEncode (<anonymous>:321:5)
at Object.Parse.Cloud.httpRequest (<anonymous>:532:24)
at SendGrid.send (sendgrid.js:292:19)
at SendGrid.sendEmail (sendgrid.js:325:17)
Please, can you help me again?
Thank you, -Francesco
Try using promise.all
pattern to iterate through your array of files then perform the sending.
https://www.promisejs.org/patterns/
(Not sure if supported by Parse.promise)
Thanks you, but unfortunately If i remove recursive method, I have the same error. The code now is:
function attachToEmailAndSend(email, index, attachments){
email.addFile("Allegato_"+(index+1), attachments[index])
.then(function(e) {
sendgrid.sendEmail(email)
.then(function(httpResponse) {
console.log(httpResponse);
},function(httpResponse) {
console.error(httpResponse);
});
});
}
and the error is the same of the last post.
Why the file object should be wrong or malformed? It's so strange...
However, thank you for the support, -Francesco
Native encodeURIComponent may throw Error if you try to encode binary file ut8 representation. It should work fine for text files though.
Hi, today I try to use the addFile function to attach a photo into an email. My code is like the README:
The images[j] is a ParseFile, and the log print the value correctly, but the email sent is without attachments. Please can you check the method or help me? Thank you, -Francesco