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

247
Visualizações
How to pass a result of a function in an array?

I want to hash a base64 to a base16 and put the result into a kvp array. Here my code (the base64 used here is hypothetical, it's to show you the problem) :

async function hashData() {

    let base64 = 'sertyuiopnbv58426931';
    let hash = await window.crypto.subtle.digest('SHA-256',  _base64ToArrayBuffer(base64));
    const hashArray = Array.from(new Uint8Array(hash));         
    const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); 
    console.log(hashHex);
    return hashHex;
}

function _base64ToArrayBuffer(mybase64) {
    let binary_string = window.atob(mybase64);
    let len = binary_string.length;
    let bytes = new Uint8Array(len);
    for (let i = 0; i < len; i++) {
        bytes[i] = binary_string.charCodeAt(i);
    }
    return bytes.buffer;
}

function GetClubData () {

    let kvp = {};
    kvp['picture'] = hashData();

    let kvp2 = {};
    kvp2['Club'] = kvp;
    
    var json = JSON.stringify(kvp2,null," ");
    
    console.log(json);

}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1"/>
  <title> Test </title>
  <link rel="stylesheet" href="test1.css"/>
</head>

<body>
  
  <button onclick="GetClubData()" > Get Club Data </button>
      
  <script src="../mylibrary/js/updateClub.js"></script>
</body>

In my console there is no 'picture' result. Any idea ?

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

0

hashData is an async function, which you need to await. The value in your "kvp" is a Promise, which cannot be JSON serialized by default.

about 4 years ago · Juan Pablo Isaza Relatório

0

your hashData function is an async function and you have to write awite if you want to access the returned value like this

function GetClubData () {
    putInArray();
}

async function putInArray() {
    let kvp = {};
    kvp['picture'] = await hashData();

    let kvp2 = {};
    kvp2['Club'] = kvp;
    
    var json = JSON.stringify(kvp2,null," ");
    console.log(json)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

If I understand you correctly, you're trying to use the result of an async function hashData inside the normal function GetClubData. You cannot do this. To fix it, make the function GetClubData async and await the result of the async function hashData. Here's the correct code,

async function GetClubData () {

    let kvp = {};
    kvp['picture'] = await hashData();

    let kvp2 = {};
    kvp2['Club'] = kvp;
    
    var json = JSON.stringify(kvp2,null," ");
    
    console.log(json);

}

Running the function GetClubData produces the result,

enter image description here

Also, call the function via onclick attribute, onclick="GetClubData()"

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