我正在尝试使用if error != nil
处理Swift中PFCloud函数的错误。虽然Cloud Code或Stripe都没有返回错误,但Swift会像响应一样回应。
夫特
PFCloud.callFunctionInBackground("authorize", withParameters: ["customer": self.customerID!, "cost": self.stripePrice, "type": self.stripeDescription) { (success: AnyObject?, error: NSError?) -> Void in
print("Authorizing")
if error != nil {
print("authorizeError")
self.displayAlert("Error", message: "There was an error authorizing your payment. Please try again later.")
} else {
if type == 1{
print("one")
self.performSegueWithIdentifier("one", sender: self)
self.viewTimer.invalidate()
self.cancelTimer.invalidate()
} else {
self.performSegueWithIdentifier("two", sender: self)
print("two")
self.viewTimer.invalidate()
self.cancelTimer.invalidate()
}
}
}
Cloud Code(Node.js)
Parse.Cloud.define("authorize", function(request, response){
var user = request.user;
stripe.charges.create({ amount: request.params.cost, currency: "cad", customer: request.params.customer, capture: false}).then(function(charge) {
user.set("chargeID", charge.id);
return user.save(null, { useMasterKey: true });
}).then(function(result) {
console.log(charge);
console.log(charge.id);
response.success(charge);
}, function(err) {
console.log(err);
console.log(charge.id)
response.error(err);
});
});
答案 0 :(得分:1)
问题是您尝试在成功回调中返回charge
并且费用为空。按如下方式更改您的代码:
stripe.charges.create({ amount: request.params.cost, currency: "cad", customer: request.params.customer, capture: false}).then(function(charge) {
user.set("chargeID", charge.id);
return user.save(null, { useMasterKey: true });
}).then(function(result) {
// charge is undefined here.
console.log(charge);
// Return something besides charge
response.success("unused result");
}, function(err) {
console.log(err);
console.log(charge.id)
response.error(err);
});