[SOLVED] Await web3.eth.getAccounts(); undefined

web3 = await Moralis.enableWeb3();
accounts = await web3.eth.getAccounts();
console.log(accounts)

the result came out like this
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ā€˜getAccountsā€™)

can you help me fix this?

if you are using latest version of Moralis SDK then you may get an ethers instance instead of an web3 instance by default

Sorry, Iā€™m kind of new to this scene. I donā€™t think I understand what you mean. Can you provide an alternative for me to look at the signed-in accounts in my inspect element console with the ā€œether instanceā€ you said?


currently following this tutorial and Iā€™m stuck with the getaccounts part I didnā€™t change or modify the code from the video, so I assume that the problem is just an updated code for ā€”>

accounts = await web3.eth.getAccounts();

you could try to read here: Moralis JS SDK v1.0 (migration to Ethers.js)

or try to use a version of the SDK that is not greater than 1.0.0

updated and Itā€™s still not working the web3.eth.getAccounts function is still not defined.

you could try to do some debugging, to see what you have on web3, there are also alternative syntaxes to get that eth address, you can also search on this forum for what solutions were found in other threads for the same problem

you can also try: web3.currentProvider.accounts[0]

web3.currentProvider.accounts[0] didnā€™t work

can you check what you have in web3? if is an web3 instance or an ethers instance

Moralis.start({ serverUrl, appId });
let web3;

async function init(){
let currentUser = Moralis.User.current();
if(!currentUser){
window.location.pathname = ā€œ/index.htmlā€;
}

web3 = await Moralis.enableWeb3();
let accounts = web3.currentProvider.accounts[0];
console.log(accounts)

const urlParams = new URLSearchParams(window.location.search);
const nftId = urlParams.get("nftId");
document.getElementById("token_id_input").value = nftId;
//document.getElementById("token_id_input").value = accounts[0];

}

async function mint(){
let tokenId = parseInt(document.getElementById(ā€œtoken_id_inputā€));
let address = document.getElementById(ā€œaddress_inputā€)
let amount = parseInt(document.getElementById(ā€œamount_inputā€))
}

init();

this code doesnā€™t say too much, it may be more important what you have in html file when importing Moralis SDK

Sure here it is.

CTYPE html>

<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="https://unpkg.com/moralis@latest/dist/moralis.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

NFT Manager

Token id
Amount
Address
Mint
<script type="text/javascript" src="./mint.js"></script>

from this line it seems that it will be an ethers instance

Ok so if itā€™s in etherā€™s instance what would be the sytax to retrieve the eth address because it seems like some of from the web3js are not working for me.

Iā€™ve been looking for it in their library but I cant seem to find it. It would be really helpful for you to help me figure it out. Thanks.

you can read there how to use web3 instead of ethers

Ok thank you cryptokid

It worked perfectly, I chose the v0.0.184 for the Moralis SDK. Seems like after the Moralis upgrade thereā€™s a problem specifically with the web3.eth.getAccounts(); function. Again, thank you so much for helping me fix my code.

2 Likes

Hey @radityaidabagus, great to have you resolved the problem :raised_hands:

it is nevertheless highly recommended that you upgrade to the latest version ASAP as new features will only be shipped in the latest ones.

the reason why web3.eth.getAccounts might not work similarly as in v0.0.184 is because there is breaking change where we move from web3js to etherjs as the default.

You still can use web3js with v>1.0.0, just need to follow the migration steps laid out up there by @cryptokid.

Cheers~