Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!