Twilio Cloud Code executing more than once

21 views
Skip to first unread message

pri...@pixnabilab.com

unread,
Jan 16, 2017, 12:13:58 PM1/16/17
to back{4}app
Hello,

I'm trying to implement twilio to my app using the cloud code, this is my code so far:

Parse.Cloud.define("sendSMSProvider",function(request,response){

 //require the Twilio module and create a REST client

 var client = require('twilio')('SID','AUTH_TOKEN');

 //Send an SMS text message

 console.log("Sending SMS");

 client.sendMessage({
 // you can use request.params to pass the number you want to send

 to:'insert_num', // Any number Twilio can deliver to
 from: 'insert_num', // A number you bought from Twilio and can use for outbound communication
 body: 'SMS From Cloud Code.' // body of the SMS message
}, {

  success:function(success){
    // console.log(success);
    response.success(success);
   },

   error:function(error){
    //  console.log(error);
     response.error(error);
   }
 });
});

It's sending me the text message, but 3 times. Through my code in app it executes the cloud code once, but I see in my logs "Sending SMS" like 4 times.
If I remove everything that involves twilio, then I see the logged "Sending SMS" just once.

I'm also receiving this error message from ios itself:
 "Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}"

Is there anything wrong that I'm doing? Any help is appreciated. 

Davi Macêdo

unread,
Jan 16, 2017, 8:21:54 PM1/16/17
to back{4}app
Hi.

Instead of:
{

  success:function(success){
    // console.log(success);
    response.success(success);
   },

   error:function(error){
    //  console.log(error);
     response.error(error);
   }
 }

try:

function(error, responseData) {
  if (error) {
    //  console.log(error);
     response.error(error);
  } else {
    // console.log(responseData);
    response.success(responseData);
  }
 }

Best!

pri...@pixnabilab.com

unread,
Jan 17, 2017, 1:19:11 PM1/17/17
to back{4}app
Yes! Thank you!
Everything is working how as intended.

I was using this exact same guide: http://docs.back4app.com/docs/integrations/using-twilio-to-send-sms-with-cloud-code/ which was giving me the error. Now I can configure it the way I want it, with params and such.

Davi Macêdo

unread,
Jan 19, 2017, 12:17:53 PM1/19/17
to back{4}app
Thanks for the feedback. We've fixed the docs.

best!
Reply all
Reply to author
Forward
0 new messages