Firebase Cloud函数请求.body为空或未定义


const functions = require("firebase-functions");
const Razorpay = require("razorpay");
const cors = require("cors")({origin: true});
exports.razorpayverification = functions.https.onRequest((req, res) => {
try {
const name = JSON.stringify(req.body);
console.log(req.body, name);
res.status(200).json({status: "ok"});
// I need req.body.amount value. req.body is empty. but JSON.stringify(req.body) contains all the data.
} catch (error) {
res.status(400).json({error: "JSON error"});
res.end();
}
});

我正在使用上述云功能,并从Razorpay webhook接收支付数据。在我的console.log中,我调用了req.body和name(const name=JSON.stringfy(req.body(;(。name以字符串格式返回数据,包含订单id、付款id、金额等所有信息,但req.body为空或未定义。我试着将以上内容包含在cors中,但结果仍然相同。请让我知道如何阅读req.body。我需要阅读req.bod.amount。

输出:

req.body:
{
name: {"entity":"event","account_id":"acc_IvsP3zm1EoP7me","event":"payment.captured","contains":["payment"],"payload":{"payment":{"entity":{"id":"pay_J9fXFPd7T9uWo5","entity":"payment","amount":1000,"currency":"INR","status":"captured","order_id":"order_J9fWySTzmrenBl","invoice_id":null,"international":false,"method":"card","amount_refunded":0,"refund_status":null,"captured":true,"description":"Recharge","card_id":"card_J9fXFSITSAyVdB","card":{"id":"card_J9fXFSITSAyVdB","entity":"card","name":"k","last4":"1111","network":"Visa","type":"debit","issuer":null,"international":false,"emi":false,"sub_type":"consumer","token_iin":null},"bank":null,"wallet":null,"vpa":null,"email":"test@test.com","contact":"+16505551234","notes":{"key1":"otzo","key2":"recharge","address":"Razorpay Corporate Office"},"fee":20,"tax":0,"error_code":null,"error_description":null,"error_source":null,"error_step":null,"error_reason":null,"acquirer_data":{"auth_code":"213319"},"created_at":1647850380}}},"created_at":1647850385}

如图所示,req.body只返回开放的大括号,之后不返回任何内容。name返回所有数据。

不确定这是正确的答案,但我的代码执行正确,并且我收到了res.body。但问题在于没有显示的console.log。如果它是一个多层次的对象,firebase cloud函数将无法正确显示。以下是Razorpays支付示例有效负载。res.body仅显示开放式花括号。res.body.payload创建了一个日志,其中包含有效负载部分下的所有详细信息。在这里,firebasecloud函数再次将每个字段显示为单独的日志。

{
"entity":"event",
"account_id":"acc_BFQ7uQEaa7j2z7",
"event":"payment.authorized",
"contains":[
"payment"
],
"payload":{
"payment":{
"entity":{
"id":"pay_DESlfW9H8K9uqM",
"entity":"payment",
"amount":100,
"currency":"INR",
"status":"authorized",
"order_id":"order_DESlLckIVRkHWj",
"invoice_id":null,
"international":false,
"method":"netbanking",
"amount_refunded":0,
"refund_status":null,
"captured":false,
"description":null,
"card_id":null,
"bank":"HDFC",
"wallet":null,
"vpa":null,
"email":"gaurav.kumar@example.com",
"contact":"+919876543210",
"notes":[

],
"fee":null,
"tax":null,
"error_code":null,
"error_description":null,
"error_source":null,
"error_step":null,
"error_reason":null,
"acquirer_data":{
"bank_transaction_id":"0125836177"
},
"created_at":1567674599
}
}
},
"created_at":1567674606
}

最新更新