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

131
Visualizações
Helper Function not returning response but shows result with the function

I have this helper function:

const listFileKeysInSpaceByPrefix = async (prefix) => {
    var params = {
        Bucket: process.env.S3_BUCKET_NAME,
        Prefix: prefix,
    };
    let fileKeys;
    await s3.listObjects(params, function (err, data) {
        if (err) {
            console.log(err, err.stack);
            next(err);
        }
        let keys = [];
        data["Contents"].forEach(function (obj) {
            keys.push(obj.Key);
        });
        console.log("key1", keys);
        fileKeys = keys;
    });
    return fileKeys;
};

And I called the helper function within a route like this:

const keyToBeDeleted = await listFileKeysInSpaceByPrefix(uploadSpaceFolder);
console.log("keys",keyToBeDeleted);

The Result on my console however looks thus:

keys undefined
key1 ['adamter/rr0.png', 'adamter/rt0.png']

I need to get the array in my response when I call the function but I seem not to find a way around this.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try with callback function.

const listFileKeysInSpaceByPrefix = (prefix, callBackFn) => {
  var params = {
    Bucket: process.env.S3_BUCKET_NAME,
    Prefix: prefix,
  };
  s3.listObjects(params, function (err, data) {
    if (err) {
        console.log(err, err.stack);
        next(err);
    }
    let keys = [];
    data["Contents"].forEach(function (obj) {
        keys.push(obj.Key);
    });
    console.log("key1", keys);
    callBackFn(keys);
  });
};

function yourFunction() { 
  function callBackFn(keyToBeDeleted) { 
    // access keyToBeDeleted 
    // add your logic here 
  }

  listFileKeysInSpaceByPrefix(uploadSpaceFolder, callBackFn); 
}
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