我有以下运行 NodeJs 8.0 的 AWS Lambda,它接收来自 API Gateway 的请求。
Lambda 代码如下所示:
const mysql = require('mysql');
exports.handler = (event, context, callback) => {
console.log("event.body = " + event.body);
console.log("event.body.requestType = " + event.body.requestType);
.
.
.
.
}
这一行:
console.log("event.body = " + event.body);
打印以下内容(在 Cloudwatch 中)
2019-03-10T16:58:31.265Z 276b4902-e716-44b1-ad9e-ed4eb4e1c02d event.body =
{
"requestType": "single",
"createdAt": "2019-03-10T16:58:29.722",
}
我想获取 requestType 的值,因此尝试执行以下操作:
console.log("event.body.requestType = " + event.body.requestType);
但它打印
event.body.createdAt = undefined
如何获取 requestType 值?
最佳答案
我认为,正文是一个编码字符串,试试这个:
console.log("event.body.requestType = " + JSON.parse(event.body).requestType);
关于javascript - 在 AWS Lambda 中获取正文请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55090257/