我有以下运行NodeJs 8.0的AWS Lambda,它从API网关接收请求。
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值?
答案 0 :(得分:1)
我认为主体是编码字符串,请尝试以下操作:
console.log("event.body.requestType = " + JSON.parse(event.body).requestType);