setTimeout(2000) before you return stopped the error for me, I literally just dealt with the same thing… only thing the balance returned null
you can check Moralis SDK version with console.log(Moralis.CoreManager.get("VERSION"))
I dont think your solution will works as the errors shows that Web3API is missing.
const fetchBotBalance = async (network, address) => {
await Moralis.initialize(secret.bsc_testnet.appId);
Moralis.serverUrl = secret.bsc_testnet.serverUrl;
user = Moralis.User.current();
console.log("user = ", user);
// user logged out properly
const options = { chain: network, address: address }; //, to_block: "10253391" };
const balances = await Moralis.Web3API.account.getTokenBalances(options);
// Error: ..Cannot read properties of undefined (reading 'account')
console.log("GOt All balances = ", balances);
return balances;
};
what error in particular you had?
where do you run that? I got an error in node
Pls read error in comments in the code
On javascript Front end
This error: //Unhandled Rejection (TypeError): Cannot read properties of undefined (reading 'account')
?
this works fine for me:
fetchBotBalance = async (network, address) => {
options = { chain: network, address: address }; //, to_block: "10253391" };
balances = await Moralis.Web3API.account.getTokenBalances(options);
console.log("GOt All balances = ", balances);
return balances;
};
To be precise, I had this code inside a function called by useEffect( async ()=>{....})
from inside React js app
the same exact error
In react you have
import { useMoralisWeb3Api } from "react-moralis";
const Web3Api = useMoralisWeb3Api();
await Web3Api.account.getTokenBalances(
yeah, this is what I maybe missing…
thanks. I will try that…
Attempted import error: ‘useMoralisWeb3Api’ is not exported from ‘react-moralis’.
I will try reinstall react-moralis for npm…
maybe u forgot the brackets {}
I copy pasted your code exactly
re installed react-moralis: still not working
A simple working example tested in codesandbox.io:
App.js
import { useMoralisWeb3Api } from "react-moralis";
function App() {
const Web3Api = useMoralisWeb3Api();
const tokenPrice = async () => {
try {
const result = await Web3Api.token.getTokenPrice({
address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
exchange: "Uniswapv2",
chain: "eth"
});
console.log(result);
} catch (e) {
console.log(e);
}
};
return (
<div>
<p>
<button onClick={tokenPrice}> test tokenPrice</button>
</p>
</div>
);
}
export default App;
index.js:
import ReactDOM from "react-dom";
import { MoralisProvider } from "react-moralis";
import App from "./App";
const rootElement = document.getElementById("root");
ReactDOM.render(
<MoralisProvider
appId="ID_HERE"
serverUrl="SERVER_URL_HERE"
>
<App />
</MoralisProvider>,
rootElement
);
NPM Uninstalling react, react-moralis, moralis then install them again seems solved the problem!
Thanks budy!