我正在尝试调用我为Mailgun编写的一些Parse CloudCode,但是当我从iOS应用程序调用它时,这就是我得到的:
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":"test@trever.me","email2":"test@trever.me","name1":"Test Test","name2":"Test Test","objectId":"KPLpKdZqSO","updatedAt":"2015-09-16T05:52:37.399Z"}}
Result: Uncaught Error: Can't form encode an Object
我从Parse控制台得到这个,有没有人知道这究竟意味着什么?
谢谢!
这是我的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 = "test@test.com";
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 */
);
这是我调用云功能的代码:
PFCloud.callFunctionInBackground("EmailConfirmations", withParameters: ["objectId": objectID, "email1" : emailCurrentUser, "email2" : emailCurrentUser, "name1" : fullnameCurrentUser, "name2" : fullnameCurrentUser])
答案 0 :(得分:1)
我猜你试图发送一个HTTP请求传递对象作为参数,它预期字符串。在传递之前使用JSON.stringify(object)
。
有关详细信息,请参阅:https://parse.com/questions/result-uncaught-error-cant-form-encode-an-object
现在我完全......不明白发生了什么。您应该重新阅读Parse Cloud指南,因为您正在将AfterSave挂钩与云函数调用混合在一起,这些是不同的东西。
我猜你的云代码应该是这样的:
// 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 = "test@test.com";
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 */
);
在快速函数调用中,您可以跳过objectID
,但应添加tradeDateTime
类型的NSDate