trever
trever

Reputation: 1003

Uncaught Error: Can't form encode an Object

I am trying to call some Parse CloudCode that I wrote for Mailgun, but when I call it from my iOS app this is what I get:

E2015-09-16T05:52:37.410Z]v4 after_save triggered for EmailConfirmations for user HjHSYVX56t:
Input: {"object":{"createdAt":"2015-09-
16T05:52:37.399Z","email1":"[email protected]","email2":"[email protected]","name1":"Test Test","name2":"Test Test","objectId":"KPLpKdZqSO","updatedAt":"2015-09-16T05:52:37.399Z"}}
Result: Uncaught Error: Can't form encode an Object

I get this from the Parse Console, does anyone know what that means exactly?

Thank you!

Here is my CloudCode:

// bring in Mailgun
var Mailgun = require('mailgun');

// init Mailgun
Mailgun.initialize('sandboxcf52f1abbe6f4bbe80c055aecc31f58f.mailgun.org', 'key-b29acfb8411921998764456e247c30fa');

Parse.Cloud.afterSave("EmailConfirmations", function(request) {
// This function runs on INSERT and DELETE operations.
// Check to ensure account is new.
if (!request.object.existed()) {
var email1 = request.object.get("email1");
var email2 = request.object.get("email2");
var name1 = request.object.get("name1");
var name2 = request.object.get("name2");
var tradeDateTime = request.object.get("tradeDateTime");
var body = "Hello " + name1 + "&" + name2 + ",\n\n Your trade on" + tradeDateTime + 
"has been approved, and will need to be processed in Empower, or via an Expresso ticket. " +
"Please process as soon as possible to ensure a successful trade.";
var sender = "[email protected]";
var subject = "Trade Confirmation";
}



Mailgun.sendEmail(
{
  to: [email1, email2],
  from: sender,
  subject: subject,
  text: body
}, onMailComplete, onMailError);

  function onMailComplete (httpResponse) {
    console.log(httpResponse);
    console.log("Email sent to " + email1 + "and" + email2);
  }

  function onMailError (httpResponse) {
    console.error(httpResponse);
    console.error("Uh oh, something went wrong");
  }



} /* eo func def */
 );

And this is the code where I'm calling the cloud function:

PFCloud.callFunctionInBackground("EmailConfirmations", withParameters: ["objectId": objectID, "email1" : emailCurrentUser, "email2" : emailCurrentUser, "name1" : fullnameCurrentUser, "name2" : fullnameCurrentUser])

Upvotes: 0

Views: 390

Answers (1)

Piotr Tobolski
Piotr Tobolski

Reputation: 1406

I guess you tried to send an HTTP request passing object as parameter where it expected string. Use JSON.stringify(object) before passing it.

For more info see: https://parse.com/questions/result-uncaught-error-cant-form-encode-an-object

EDIT:

Now i totally.. don't understand what's going on. You should re-read the Parse Cloud guide because you are mixing afterSave hooks with cloud function calls and those are different things.

I guess your cloud code should look like this:

// bring in Mailgun
var Mailgun = require('mailgun');

// init Mailgun
Mailgun.initialize('sandboxcf52f1abbe6f4bbe80c055aecc31f58f.mailgun.org', 'key-b29acfb8411921998764456e247c30fa');

Parse.Cloud.define("EmailConfirmations", function(request) {

var email1 = request.params.email1;
var email2 = request.params.email2;
var name1 = request.params.name1;
var name2 = request.params.name2;
var tradeDateTime = request.params.tradeDateTime;
var body = "Hello " + name1 + "&" + name2 + ",\n\n Your trade on" + tradeDateTime + 
"has been approved, and will need to be processed in Empower, or via an Expresso ticket. " +
"Please process as soon as possible to ensure a successful trade.";
var sender = "[email protected]";
var subject = "Trade Confirmation";

Mailgun.sendEmail({
  to: [email1, email2],
  from: sender,
  subject: subject,
  text: body
}, onMailComplete, onMailError);

function onMailComplete (httpResponse) {
    console.log(httpResponse);
    console.log("Email sent to " + email1 + "and" + email2);
}

function onMailError (httpResponse) {
    console.error(httpResponse);
    console.error("Uh oh, something went wrong");
}

} /* eo func def */
);

And in the swift function call you can skip the objectID but you should add tradeDateTime of type NSDate

Upvotes: 1

Related Questions