Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda