[SOLVED] Unhandled Rejection (Error): You need to call Parse.initialize before using Parse

Does anyone know why this code is giving me trouble? It was working earlier now the page won’t even load for 2 seconds without getting above error

Thanks guys

import React, {useState, useEffect} from 'react'
import './Send.css'
import {Button} from 'react-bootstrap';
import { useMoralisWeb3Api } from "react-moralis";



const Send = () => {

  const  Web3Api  = useMoralisWeb3Api();
  const [nativeNetwork, setNativeNetwork] = useState();
  const [nativeBalance, setNativeBalance] = useState();

  const fetchNetwork =  () => {
    if (window.ethereum.chainId === "0x539") {
    setNativeNetwork('Ganache');
  } else if (window.ethereum.chainId === "0x1") {
    setNativeNetwork('Ethereum')
  } else if (window.ethereum.chainId === "0x38") {
    setNativeNetwork('Binance')
  } else if (window.ethereum.chainId === "0x89") {
    setNativeNetwork('Polygon')
  } else if (window.ethereum.chainId === "0xa86a") {
    setNativeNetwork('Avalanche')
  } else if (window.ethereum.chainId === "0xa4b1") {
    setNativeNetwork('Arbitrum(L2)')
  } else {
    setNativeNetwork('Not Supported')
  }
  };



  useEffect(() => {
    const fetchBalance = async () => {
      let result = await Web3Api.account.getNativeBalance({ address: window.ethereum._state.accounts[0]})
      result = result.balance;
      if (window.ethereum._state.accounts > [""]) {
          setNativeBalance(result);
        } else {
      setNativeBalance('Disconnected')
    }};
    fetchNetwork()
    fetchBalance()
  }, [Web3Api.account])

return(...)
}

export default Send

also when I change the following line:

  const   Web3Api  = useMoralisWeb3Api();

To:

  const  { Web3Api } = useMoralisWeb3Api();

the new error I get is:

TypeError: Cannot read properties of undefined (reading 'account')

My metamask is unlocked and connected…

1 Like

You need to call Parse.initialize before using Parse error usually is because Moralis serverUrl and appId were not set.

That info has been correct since I started the project, I haven’t touched it lately and I just copy and pasted again from admin.moralis.io for good measure… still getting same error

do I have to enable() anything before using Web3Api?

working example:
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="APP_ID"
    serverUrl="SERVER_URL"
  >
    <App />
  </MoralisProvider>,
  rootElement
);

App.js

import { useMoralisWeb3Api } from "react-moralis";

function App() {
  const Web3Api = useMoralisWeb3Api();

  const x = async () => {
    try {
      const result = await Web3Api.account.getNativeBalance({
        address: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
        chain: "eth"
      });
      console.log(result);
    } catch (e) {
      console.log(e);
    }
  };

  return (
    <div>
      <p>
        <button onClick={x}> test button</button>
      </p>
    </div>
  );
}

export default App;

This is nice to see but I’m afraid viewing this didn’t help

if you create a new react project with this code and with latest Moralis SDK (0.0.97) version and react-moralis (0.2.6 now) then it should work.

Ok understood, let me rephrase… this code works PERFECT initially (when I save my project and run npm start) however when I press refresh is when I run into problems

I don’t get what is the difference from when you hit refresh and when you start the application initially.

Yea I’m in the same boat, that’s why I reached out to the professionals… does it matter that I connected my app to metamask through:

const Moralis = require('moralis')

and not useMoralis() ?

A user is logged in at the same time as well with just email (using useMoralis()) but no official address is linked in the user database

@Yomoo I know you’re an expert too wya bro! help!

1 Like

Hi @wesleyt95

I’ll show you part of my code where I use web3API on react-moralis on the latest versions.

import { useMoralisWeb3Api, useMoralis  } from "react-moralis";

const ERC20Transfers = () => {
  const { account } = useMoralisWeb3Api();
  const { isInitialized} = useMoralis ();

  useEffect(() => {
    if (isInitialized)
      fetchERC20Transfers()
        .then((balance) => console.log(balance))
        .catch((e) => alert(e.message));
  }, [isInitialized]);
  
const fetchERC20Transfers = async (options) => {
    return await account
      .getTokenTransfers({ address: walletAddress, chain: "bsc" })
      .then((result) => result.result)
      .catch((e) => alert(e.message));
  };
};

I guess this will solve your problem. If it will not - share your repo and we will find the problem :man_factory_worker:

Yomoo!

The code works (sometimes), it’s just that whenever I press refresh I get:

×
Unhandled Rejection (Error): You need to call Parse.initialize before using Parse.

I have a line similar to isInitialized but either way I’m logged into my (non-crypto) account AND my metamask is currently connected… Here’s my code, it works fine at first but I don’t know where it went wrong

import React, {useState, useEffect} from 'react'
import './Send.css'
import {Button} from 'react-bootstrap';
import { useMoralisWeb3Api } from "react-moralis";



const Send = () => {
  const { account }  = useMoralisWeb3Api();
  const [nativeNetwork, setNativeNetwork] = useState();
  const [nativeBalance, setNativeBalance] = useState();
  
  const fetchNetwork =  () => {
    if (window.ethereum.chainId === "0x539") {
    setNativeNetwork('Ganache');
  } else if (window.ethereum.chainId === "0x1") {
    setNativeNetwork('Ethereum')
  } else if (window.ethereum.chainId === "0x38") {
    setNativeNetwork('Binance')
  } else if (window.ethereum.chainId === "0x89") {
    setNativeNetwork('Polygon')
  } else if (window.ethereum.chainId === "0xa86a") {
    setNativeNetwork('Avalanche')
  } else if (window.ethereum.chainId === "0xa4b1") {
    setNativeNetwork('Arbitrum(L2)')
  } else {
    setNativeNetwork('Not Supported')
  }
  };



  useEffect(() => {
    const fetchBalance = async () => {
      let result =   await account.getNativeBalance({ address: "0xAb97Bf09F0F404f9dc271314bb09Dd0f097C990A"})
      result = result.balance;
      if (window.ethereum._state.accounts > [""]) {
          setNativeBalance(result);
        } else {
      setNativeBalance('Disconnected')
    }};
    fetchNetwork()
    fetchBalance()
  }, [account])

Thanks

btw the user is logged in using non-crypto (react-moralis) but connected to ethereum through:

const Moralis = require('moralis')

Moralis.enable()

the addresses arent linked to user database… idk if that matters

You guys can close this thx, I had to switch from useMoralisWeb3Api to just web3… strange because I use the same function 3 other places with no problems

1 Like