Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
Function argument in nodejs

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')    
    }
  }
  
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!