running Lambda test with
{
"var1": "2017-04-17T18:48:03.608Z",
"var2": "0.45",
"var3": "0.5"
}
Function:
exports.handler = (event, context, callback) => {
console.log("event.body = " + event.body);
const {var1, var2, var3} = JSON.parse(event.body);
const tmpItem = {
"var_1": var1,
"var_2": var2,
"var_3": var3
};
console.log('Inserting item');
}
gives the following exception:
Request ID: "3aa87175-d544-11e8-ab0a-2b268a563fb1"
Function Logs:
START RequestId: 3aa87175-d544-11e8-ab0a-2b268a563fb1 Version: $LATEST
2018-10-21T15:16:05.617Z 3aa87175-d544-11e8-ab0a-2b268a563fb1 event.body = undefined
2018-10-21T15:16:05.636Z 3aa87175-d544-11e8-ab0a-2b268a563fb1 SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at exports.handler (/var/task/index.js:18:89)
END RequestId: 3aa87175-d544-11e8-ab0a-2b268a563fb1
REPORT RequestId: 3aa87175-d544-11e8-ab0a-2b268a563fb1 Duration: 82.98 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 25 MB
RequestId: 3aa87175-d544-11e8-ab0a-2b268a563fb1 Process exited before completing request
What seems to be the issue?
This is because when you test your lambda with a packet, say
{
"var1": "2017-04-17T18:48:03.608Z",
"var2": "0.45",
"var3": "0.5"
}
Then that packet is passed as the event
to the handler.
However, you are doing JSON.parse(event.body)
, but for the above packet, event.body
is undefined
(which has token u
at position 0).
You should change your test packet to:
{
"body": "{\"var1\":\"2017-04-17T18:48:03.608Z\",\"var2\":\"0.45\",\"var3\":\"0.5\"}"
}
Note that the body is a stringified JSON, because API Gateway Lambdas expect event body in a stringified format.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With