0

This line of code returns a new address. web3.eth.personal.newAccount('!@superpassword').then(console.log);

but how is it possible to get this address in a variable, So it can be used for inserting into database

  • const myAddress = await web3.eth.personal.newAccount('!@superpassword'); should work – Ismael Mar 17 '18 at 23:05
  • give give me err. is there any complete code example? const myAddress = await web3.eth.personal.newAccount('!@superpassword'); ^^^^^ SyntaxError: await is only valid in async function at new Script (vm.js:51:7) at createScript (vm.js:136:10) at Object.runInThisContext (vm.js:197:10) at Module._compile (module.js:613:28) at Object.Module._extensions..js (module.js:660:10) at Module.load (module.js:561:32) at tryModuleLoad (module.js:501:12) at Function.Module._load (module.js:493:3) – shawn Mar 18 '18 at 11:08
  • 1
    This is a javascript problem, you need to understand how variables works, how to use promises, and how to call functions with async/await. A solution without async/await is var myAddress; web3.eth.personal.newAccount('!@superpassword').then((result) => { myAddress = result; }). I'd suggest to learn how javascript deals with async functions. – Ismael Mar 18 '18 at 20:43
  • i have go it working – shawn Mar 19 '18 at 10:02
0

If you’re creating an asynchronous request you can retrieve the response using a callback or a promise

web3.personal.newAccount("passphrase").then((accountAddress) => { console.log('New Account Address', accountAddress); })
  • if i run this code like var accountAddress; web3.eth.personal.newAccount("!@superpassword").then((accountAddress) => { console.log('New Account Address', accountAddress); }); console.log("New Address : ", accountAddress); i get following output ` New Address : undefined New Account Address 0x485BF2C4981Bb8a15269164FEf28eb2f57dD2955 ` Why do i see undefine here? – shawn Mar 17 '18 at 12:36
  • Is there any example for promise too? – shawn Mar 17 '18 at 12:45
  • Yes, an example is given in docs – Ayushya Mar 18 '18 at 4:26
  • Do you have an example for me to get address in a value using callback or promise? im fairly new – shawn Mar 18 '18 at 11:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.