无法将随机数发送到 Braintree

can't send nonce to Braintree

我正在尝试与 Brain tree 沙箱集成,

我正在尝试将随机数从 ios 客户端发送到我的服务器,但我在控制台中收到此错误:TypeError: Cannot read property 'payment_method_nonce' of undefined

我的服务器端代码是:

app.post("/checkout", function (req, res) {
var nonceFromTheClient = req.body.payment_method_nonce;
gateway.transaction.sale({
amount: "10.00",
paymentMethodNonce: nonceFromTheClient,
options: {
  submitForSettlement: true
}
}, function (err, result) {
if (err) {
  print(err)
}
else if (result.success) {
  console.log(result);
}
});});

在我的 Ios 那边:

    func createTransaction(paymentMethodNonce:String) {
    let paymentURL = postUrl!
    var request = URLRequest(url: paymentURL)
    request.httpBody = "payment_method_nonce=\(paymentMethodNonce)".data(using: String.Encoding.utf8)
    request.httpMethod = "POST"

    URLSession.shared.dataTask(with: request) { (data, response, error) -> Void in
        if let err = error {
            print(err.localizedDescription)
            return}
        else {
            print(response)}
        }.resume()
}

TypeError: Cannot read property 'payment_method_nonce' of undefined

这意味着您的 req.body 已被 empty/not 解析。您是否导入了 body-parser?此模块将解析正文并提供给您。