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

185
Visualizações
How to see account address in another function in Javasript

The account address is getting displayed on the console properly when initializing but showing undefined when calling console.log(account) from other functions. Here is my code.

var account;
window.addEventListener('load', async () => {
    if (window.ethereum) {
        window.web3 = new Web3(ethereum);

        ethereum.autoRefreshOnNetworkChange = false;

        const accounts = await ethereum.enable();
        account = accounts[0];
        console.log(account); //here the account working properly.
    }
});

var contractaddress = '0x26708Df214A65Dda444E61266642e6650F4e8923';

function get_request_details() {
    console.log(account); //here it is showing undefined
}
window.onload = get_request_details;
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The problem was arising because the function was running before the initialization can be done and that is why I modified the code as follows.

var account;
window.addEventListener('load', async () => {
    if (window.ethereum) {
        window.web3 = new Web3(ethereum);

        ethereum.autoRefreshOnNetworkChange = false;

        const accounts = await ethereum.enable();
        account = accounts[0];
        console.log(account); //here the account working properly.
        get_request_details();
    }
});

var contractaddress = '0x26708Df214A65Dda444E61266642e6650F4e8923';

function get_request_details() {
    console.log(account); //here it is also working
}

I called the function from the async event only so that when the initialization will be done after than only the function should run.

about 4 years ago · Juan Pablo Isaza Relatório

0

I don't see the contractaddress variable used anywhere. So here is my opinion.

const get_request_details = function(account) {
    console.log(account);
}

const run_after_window_loaded = async function() {
    if (!window.ethereum) {
        return false;
    }
    window.web3 = new Web3(ethereum);
    ethereum.autoRefreshOnNetworkChange = false;

    const accounts = await ethereum.enable();
    console.log(accounts);

    if(accounts.length > 0){
        get_request_details(accounts[0]);
    }
}

window.addEventListener('DOMContentLoaded', async function(event) {
    await run_after_window_loaded();
});
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