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

164
Vistas
Getting undefined back from AWS secrets call

I've seen this question around, but it seems nobody is having the same problem as me. I followed the AWS docs and implemented their code the way they told me to, but I keep getting undefined back?

Here's the code:

// Load the AWS SDK
var AWS = require('aws-sdk'),
    region = "myRegion",
    secretName = "myARN",
    secret,
    decodedBinarySecret;

// Create a Secrets Manager client
var client = new AWS.SecretsManager({
    region: region
});

// In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
// See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
// We rethrow the exception by default.

client.getSecretValue({SecretId: secretName}, function(err, data) {
    if (err) {
        if (err.code === 'DecryptionFailureException')
            // Secrets Manager can't decrypt the protected secret text using the provided KMS key.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw err;
        else if (err.code === 'InternalServiceErrorException')
            // An error occurred on the server side.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw err;
        else if (err.code === 'InvalidParameterException')
            // You provided an invalid value for a parameter.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw err;
        else if (err.code === 'InvalidRequestException')
            // You provided a parameter value that is not valid for the current state of the resource.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw err;
        else if (err.code === 'ResourceNotFoundException')
            // We can't find the resource that you asked for.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw err;
    }
    else {
        // Decrypts secret using the associated KMS key.
        // Depending on whether the secret is a string or binary, one of these fields will be populated.
        if ('SecretString' in data) {
            let secret = data.SecretString;
            secret = JSON.parse(secret);
        } else {
            let buff = Buffer.from(data.SecretBinary, 'base64');
            decodedBinarySecret = buff.toString('ascii');
        }
    }
    
    // Your code goes here.
    // This is where undefined gets spit out
    console.log(decodedBinarySecret);
    console.log(secret);
});

There must be something that I'm missing / not doing?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It seems that in the outer else part of your conditional statement you're re-declaring the secret variable which you've also declared at the very top. That secret variable gets a value but only inside the nested if body not outside. Which keeps the secret variable still undefined.

Also, according to your code either variable secret will have some value or decodedBinarySecret. One of them will be undefined.

Try not to re-declare the variable inside the body. See below code.

 else {
    // Decrypts secret using the associated KMS key.
    // Depending on whether the secret is a string or binary...
    if ('SecretString' in data) {
        secret = data.SecretString;
        secret = JSON.parse(secret);
    } else {
        let buff = Buffer.from(data.SecretBinary, 'base64');
        decodedBinarySecret = buff.toString('ascii');
    }
}
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