0

I have simple javascript in parse's cloud code to collect list of email addresses which I add to my httprequest in cloudcode to send to mandrill.

However, the http request seem to have extra backslashes(\) causing mandrill to reject the request. Nevertheless, then I print the same string in alert box in cloude code, it prints it perfectly. I have compared the httprequest log to hardedcoded email list request (that request succeeds), and hence I know about these extra chars.

I am really not sure what is happening, any pointers would be highly appreciated.

I have snippets of my code and parts of log as below:

Cloud code to collect email in a string: . .

var query1 = new Parse.Query(Parse.User);
                       query1.equalTo("emailVerified", true);
                       query1.find({
                           success: function (object)
                           {
                                 var emaillink;
                               for (var i=0;i<object.length;i++)
                               {
                                   var testresult=object[i];
                                   if(i==0)
                                   {
                                     emaillink="\'"+testresult.get("email")+"\', \"name\" : \'"+testresult.get("username")+"\', \"type\" : \'to\' },";

                                   }
                                   else
                                   {
                                     emaillink=emaillink+"{ \"email\" : \'"+testresult.get("email")+"\', \"name\" : \'"+testresult.get("username")+"\', \"type\" : \'to\' },"
                                   }

                               }
                               emaillink = emaillink.substring(0, emaillink.length - 2);
                               alert(emaillink);
                               Parse.Cloud.httpRequest({...

cloudcode to add these emails to httprequest format for mandrill:

. . .

   "to": [
           {  "email": emaillink
           }
          ]

. .

log of alert:

##################################################
# Alert                                          #
##################################################
#                                                #
# 'abc@gmail.com', "name" : 'abc               1 #
# ', "type" : 'to' },{ "email" : 'abcdefghijklnm #
# 101@gmail.com', "name" : 'abcdefgh', "type" :  #
# 'to' },{ "email" : 'qwertyiopau@yahoo.com', "n #
# ame" : 'qwertyiop', "type" : 'to'              #
#                                                #
#                                         [ OK ] #
#                                                #
##################################################

log in cloudcode of the httprequest:

..."text":"[{\"email\":\"'abc@gmail.com', \\\"name\\\" : 'abc     1', \\\"type\\\" : 'to' },{ \\\"email\\\" : 'abcdefghijklnm101@gmail.com', \\\"name\\\" : 'abcdefghijklnm', \\\"type\\\" : 'to' },{ \\\"email\\\" : 'abcdefghijklnm@yahoo.in', \\\"name\\\" : 'abcdefghijklnm', \\\"type\\\" : 'to'\",\"status\":\"invalid\",\"_id\":\"18523fc4a3d24a1398c303972af47cd8\",\"reject_reason\":null}]","data":[{"email":"'abc@gmail.com', \"name\" : 'abc', \"type\" : 'to' },{ \"email\" : 'abcdefghijklnm101@gmail.com', \"name\" : 'abcdefghijklnm', \"type\" : 'to' },{ \"email\" : 'abcdefghijklnm@y.in', \"name\" : 'abcdefghijklnm', \"type\" : 'to'","status":"invalid","_id":"18523fc4a3d24a1398c303972af47cd8","reject_reason":null}],"buffer":{"0":91....

please ignore the emails, i have replaced actual emails with some random email values. The values are correct, that I have checked.

It is just these extra \ that I can't explain. Any help is highly appreciated. Thanks

RainMaker
  • 1
  • 1

1 Answers1

0

You are not building your array the right way. Here is an example for your case:

...
var emaillink = [] // Initialize an empty array;
for (var i=0;i<object.length;i++) 
{
    var testresult = object[i];

    emaillink.push({                        // We push each email in our array
        email: testresult.get("email"),
        name: testresult.get("username"),
        type: "to"
    })
}
...

This is how you build a JSON Object, you don't need to create a string. If you want to get the string of your json array, you just have to do:

emaillink.toString();

So you just have to pass this object to your httpRequest. You probably don't have to do a 'toString()' just pass the object, httpRequest will stringify it.

To completely answer your question, the extra '\' are escape characters. You used '\' to escape your '"' in your string, when sending your string httpRequest escaped your characters too. That why it add some '\' to escape yours '\'.