When console logging the event object using JSON.stringify(event) I get the below output.
{
...
"body": "{\"vox\":{\"id\":10431,\"summary_text\":\"Vox received from 978-1913905101 via Book reviews at 17:17 (UTC) on 26/01/2022\",\"send_time\":\"2022-01-26T17:17:51.812Z\",\"contact_email\":\"\",\"contact_phone\":\"\",\"channel_name\":\"Book reviews\",\"channel_public_name\":\"telbee\",\"duration\":1,\"vox_context\":null,\"channel_id\":2786,\"recording_url\":\"https://api.telbee.io/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBczh6IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--9fdab42822e2a9235f03ca178ae917a83941b6cd/Wed Jan 26 2022 19-17-51 GMT 0200 (Eastern European Standard Time)\",\"direction\":\"vox_in\",\"channel_status\":\"active\",\"transcription_state\":\"not_transcribed\",\"share_link\":\"https://app.telbee.io/vox/Djw3em5mEMse6yjM5aA4S6oM\",\"vox_no\":2,\"sending_user\":null,\"contact_name\":\"978-1913905101\",\"contact_id\":6275,\"actual_transcription\":\"\",\"transcription_language\":\"en-US\",\"conversation_inbox_url\":\"https://app.telbee.io/inbox?token=iP9jHf2KVVrZuZRoe7n9NFLT\",\"sending_user_id\":null}}",
"isBase64Encoded": false
}
But when I try to get the vox element of the body it fails
console.log('event body = ' JSON.parse(event.body.vox.contact_name));
The error message is
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'contact_name' of undefined",
"stack": [
"TypeError: Cannot read property 'contact_name' of undefined",
" at Runtime.exports.handler (/var/task/index.js:49:69)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}
I'm not sure how to get access to event.body.vox.contact_name even though I know it's there in the payload I cant get access to it. I know I have to parse it first as it is escaped but the console.log does not allow me to access the event objects body element.
I'm not sure what the issue could be as this is the event payload that is passed into this lambda function that I am creating and is attached to an AWS Gateway.
CodePudding user response:
You are trying to get a value before parsing.Try this
var body=JSON.parse(event.body);
var contactName=body.vox.contact_name; // 978-1913905101
