Variable when passed through a function returns property of value undefined. I'm passing variable ApiId
or ApiName
. Should the variable name
be wrapped some other way inside the function?
Error:
ERROR Invoke Error
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'value' of undefined",
"stack": [
"TypeError: Cannot read property 'value' of undefined",
" at dimensionValue (/var/task/index.js:20:80)",
" at Runtime.exports.handler (/var/task/index.js:37:36)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
}
code:
function dimensionValue(name) {
console.log("Testing")
//var dimensionsValue = sns.Trigger.Dimensions.find(dimension => dimension.name === name).value
return sns.Trigger.Dimensions.find(dimension => dimension.name === name).value
}
if (sns.Trigger.Namespace == "AWS/ApiGateway") {
console.log("Testing2")
if (sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiId') && sns.Trigger.Dimensions.find(dimension => dimension.name === 'Stage')) {
console.log('ApiId and Stage')
var sns_DimensionsValue = dimensionValue('ApiId') '_' dimensionValue('Stage')
} else if (sns_DimensionsValue == sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiName') && sns.Trigger.Dimensions.find(dimension => dimension.name === 'Stage')) {
console.log('ApiName and Stage')
var sns_DimensionsValue = dimensionValue('ApiName') '_' dimensionValue('Stage')
} else if (sns_DimensionsValue == sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiId')) {
console.log('ApiId')
var sns_DimensionsValue = dimensionValue('ApiId')
} else if (sns_DimensionsValue == sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiName')) {
console.log('ApiName')
var sns_DimensionsValue = dimensionValue('ApiName')
}
}
CodePudding user response:
The problem are those sns_DimensionsValue ==
parts at the start of every condition. Just remove them.
You essentially do if (undefined == findAppId() && findStage())
, which will be true if Stage was found and AppId wasn't, but then you attempt to access the value of AppId and fail.
Let me suggest a generally simplified approach:
const dimensions = Object.fromEntries(sns.Trigger.Dimensions.map(d => [d.name, d.value]))
let sns_DimensionsValue = dimensions.ApiId ?? dimensions.ApiName
if (!sns_DimensionsValue) throw new Error('No API ID/API name found')
if (dimensions.Stage) sns_DimensionsValue = '_' dimensions.Stage