0

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?

I'm using this answer method and using it inside a function.

   function dimensionValue(name) {
       console.log("Testing")
       return sns.Trigger.Dimensions.find(dimension => dimension.name === name).value
   }

If I try to console.log then it works fine.

console.log(sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiId').value)

enter image description here

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')    
    }
  }
  
user630702
  • 2,529
  • 5
  • 35
  • 98
  • 2
    The error means that `sns.Trigger.Dimensions` doesn't contain an element that fulfills `dimension.name === name`. You have to ask yourself: What should happen if the list does not contain an element with that name? And then implement that. Right now you seem to assume that there will always be an element with that name, but since you are getting this error, that's clearly not the case. – Felix Kling Nov 12 '21 at 09:32
  • I'm afraid I don't understand the question. It would appear that `find` doesn't find anything, so it returns `undefined`. You're trying to use `.value` on `undefined`, which causes an error. You have to handle the situation where nothing is found. – T.J. Crowder Nov 12 '21 at 09:32
  • Never mind. Its the typo in if condition. I'm trying to assign variable in if condition instead of checking if it exists. – user630702 Nov 12 '21 at 09:44
  • You're trying to read `sns_DimensionsValue` when it hasnt been set (in the `else if` conditions) – Jamiec Nov 12 '21 at 09:45
  • Unrelated but you're also switching between using your method `dimensionValue` and rewriting the same code `sns.Trigger.Dimensions.find(dimension => dimension.name === 'ApiName')` – Jamiec Nov 12 '21 at 09:47
  • `dimensionsValue` function returns the value ok the key. I'll have to create another for `CheckIfdimensionsNameExists` if I don't have to rewrite the long string – user630702 Nov 12 '21 at 09:48

1 Answers1

2

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
CherryDT
  • 25,571
  • 5
  • 49
  • 74