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

219
Vistas
Web3 code working in terminal but not my IDE

I am very new to web3 and relatively new to JavaScript. The following code returns undefined for the variable accounts.

Here is my code:

const Web3= require('web3');
var web3 = new Web3("HTTP://127.0.0.1:7545");
var accounts; 
web3.eth.getAccounts().then(acc=>{accounts = acc});
console.log(accounts);

The code works completely in the terminal, printing out the list of accounts. However, it says undefined when I run it in the IDE. I have tried multiple things, such as even declaring a new function to be called from the web3.eth.getAccounts() as a callback, setting the account variable. Nothing works.

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

0

The getAccounts() function returns a promise which resolves later than the console.log, so before the accounts variable has been set.

You can either use the accounts variable after it's been set (watch out for the callback hell)

web3.eth.getAccounts().then(acc => {
    accounts = acc;
    console.log(accounts);
});

or use the await expression in an async function

async function run() {
    var accounts;
    accounts = await web3.eth.getAccounts();
    console.log(accounts);
}

run();
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