[SOLVED] web3API: Cannot read properties of undefined (reading 'account')

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!

2 Likes