【问题标题】:Swift wrongly reading Cloud Code errorSwift 错误读取 Cloud Code 错误
【发布时间】:2017-09-06 09:29:01
【问题描述】:

我正在尝试使用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()
        }
    }
}

云代码 (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);
    });
});

【问题讨论】:

    标签: node.js swift stripe-payments parse-server parse-cloud-code


    【解决方案1】:

    问题是您试图在成功回调中返回charge 并且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);
    });
    

    【讨论】:

    • 是的,没有错误。在 Stripe 这边成功创建了一笔费用。
    • 使用 Stripe 创建费用并不意味着没有错误。用户是否使用正确的chargeId 保存?当在线console.log(charge.id) 时,控制台会打印什么?我怀疑charge.id 未定义,这会导致运行时错误并导致response.success() 永远不会被执行。
    • 谢谢。我删除了 charge.id 行,但它仍然表现得好像有错误一样。你还建议我改变什么?
    • 你能在解析服务器日志中查看console.log(charge) 执行时打印到控制台的内容吗?还可以尝试在 Swift 代码中打印出错误。而不是print("authorizeError") 尝试print("authorizeError: \(error) ") 以查看错误内容。
    • @Dups 另外,用户是否使用正确的 ChargeId 保存?
    猜你喜欢
    • 2016-01-10
    • 2021-12-06
    • 1970-01-01
    • 2021-03-02
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多