Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda