我正在尝试与Brain tree sandbox集成,
我正在尝试将我的nonce从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()
}
答案 0 :(得分:2)
TypeError: Cannot read property 'payment_method_nonce' of undefined
这意味着您的req.body为空/未解析。你导入了body-parser吗?该模块将解析正文并使其可供您使用。