Koolstr
Koolstr

Reputation: 502

Convert Java string array or JSON string to Javascript Array (Parse.com Cloud Code and Mandrill)

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.

Upvotes: 1

Views: 2743

Answers (1)

Koolstr
Koolstr

Reputation: 502

I finally figured it out on my own. It turns out the passed-in JSON string needed to be parsed by JSON to properly format it as a Javascript array. This is what I did:

var recipientJavaArray = request.params.toEmail;
var recipientArray = JSON.parse(recipientJavaArray);

Then I was wrong in trying to create a two-dimensional array with key->value structure. I found out that arrays in Javascript don't support keys - that is an Object in Javascript. So I changed the for loop to create recipient Objects to store in the Array, rather than arrays in the Array.

var recipients = [];
for (var i = 0; i < recipientArray.length; i++) {
    var recipientObject = {};
    recipientObject["email"] = recipientArray[i];
    recipients.push(recipientObject);
}

I hope this helps people who have had a similar problem. Here is the complete, correct function for future reference by others and for convenience:

Parse.Cloud.define("sendEmail", function(request, response) {
var recipientJavaArray = request.params.toEmail;
var recipientArray = JSON.parse(recipientJavaArray);
var recipients = [];
for (var i = 0; i < recipientArray.length; i++) {
    var recipientObject = {};
    recipientObject["email"] = recipientArray[i];
    recipients.push(recipientObject);
}

var Mandrill = require('mandrill');
Mandrill.initialize('1C7****************fOQ');
Mandrill.sendEmail({
    message: {
        text: request.params.text,
        subject: request.params.subject,
        from_email: request.params.fromEmail,
        from_name: request.params.fromName,
        to: recipients
    },
    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");
    }
});
});

Upvotes: 1

Related Questions