I am trying to make an array in Javascript (for my Parse.com Cloud Code) that would enable me to pair each string from a String array I am passing from my java application to a key called "email". This way, I can set the Mandrill's message-to-send's 'to' property to multiple emails.
This first line of my function, var recipientJavaArray = request.params.toEmail.split(",");
is my attempt to convert the passed in Java JSON string (which was formatted from a Java string array) into a Javascript array of strings.
The for loop right afterwards is my attempt to then create a new two-dimensional array in which they are essentially key-value pairs: "email"=>"[email protected]"
What is it I am doing wrong in this conversion process, that it doesn't seem to be working? The console log still reports it as a success as having sent the email to send to Mandrill's servers:
I2014-07-10T06:34:40.618Z] v8: Ran cloud function sendEmail with: Input: {"fromEmail":"k*****@gmail.com","toEmail":"[\"k*****@gmail.com\",\"a***********@gmail.com\"]","text":"Sample test text","fromName":"Sample Test Name","toName":"Sample Recipient Name","subject":"test 5"} Result: Email sent!
This is my Java code for the passed-in JSON string, and how I converted the string array to a JSON string. Maybe I did something wrong here?
Gson converter = new Gson();
String recipientsInJson = converter.toJson(recipients);
params.put("toEmail", recipientsInJson);
The full Javascript function is below, for context:
Parse.Cloud.define("sendEmail", function(request, response) {
var recipientJavaArray = request.params.toEmail.split(",");
var recipientArray = new Array();
for (var i = 0; i < recipientJavaArray.length; i++) {
var oneRecipient = new Array();
oneRecipient["email"] = recipientJavaArray[i];
recipientArray.push(oneRecipient);
}
var Mandrill = require('mandrill');
Mandrill.initialize('1C7****************OQ');
Mandrill.sendEmail({
message: {
text: request.params.text,
subject: request.params.subject,
from_email: request.params.fromEmail,
from_name: request.params.fromName,
/*THIS is what I am trying to get to work. It is an array struct.*/
to: recipientArray
},
async: true
},{
success: function(httpResponse) {
console.log(httpResponse);
response.success("Email sent!");
},
error: function(httpResponse) {
console.error(httpResponse);
response.error("Uh oh, something went wrong");
}
}); });
Documentation on what exactly the 'to' parameter in Mandrill is and what it is made up of, can be found here: https://mandrillapp.com/api/docs/messages.html
Maybe there is a better way to do this than the way I currently am trying to do? Like fitting the email strings with their "email" key in an array (or HashMap?) in Java and bringing it over to Javascript and converting that back to a standard Javascript array? Is that even possible? If so, how would I do that? Any help would be greatly appreciated.