Hi, I’m having a sudden error when using the web3api.
I’m trying to call
NFTOwners = await Moralis.Web3API.account.getNFTsForContract(options);
but it always throws the error "Error: Web3Api not initialized, run Moralis.start() first"
.
I did initialize Moralis with that function and the correct AppId and ServerUrl because all the other calls (Moralis.User.current(), Moralis.Session.current() etc…) work.
Can you help me with this please?
I also looked into Initialized, run Web3Api.initialize first and Web3Api not initialized before posting but they did not solve my issue.
I have moralis version 0.0.183 and the server is version 0.0.326.
Thank you for your time!
Giovanni
post the code how you init thanks
Sorry, here you are.
Basically before the components that calls getNFTsForContract
is mounted this function is run:
const user = await startMoralis(loginInfo);
await attemptLogin(user, dispatch);
where startMoralis
has Moralis.start()
in it and attemptLogin
tries to use Moralis.authenticate
and gets login info. After these functions have run, the component with the call getNFTsForContract
is mounted and so the function is run.
loginInfo = {serverUrl: '...', appId: '...'}
export const startMoralis: (arg: { serverUrl: string; appId: string }) => Promise<Moralis.User | undefined> = async ({ serverUrl, appId }) => {
await Moralis.start({ serverUrl, appId });
return await Moralis.User.current();
};
export const attemptLogin = async (user: Moralis.User | undefined, dispatch: Function, provider?: string) => {
if (!user) {
if (provider === 'walletconnect') {
//@ts-ignore
user = await Moralis.authenticate({ provider });
// @ts-ignore
await Moralis.enableWeb3({ provider });
} else {
//@ts-ignore
user = await Moralis.authenticate({});
// @ts-ignore
await Moralis.enableWeb3();
}
}
if (user) {
if (provider === 'walletconnect') {
// @ts-ignore
await Moralis.enableWeb3({ provider });
} else {
// @ts-ignore
await Moralis.enableWeb3();
}
//@ts-ignore
await Moralis.initPlugins();
const session = await Moralis.Session.current();
try {
// @ts-ignore
await Moralis.switchNetwork('0x38');
} catch (e) {
console.log(e);
const chainId = 56;
const chainName = 'Binance Smart Chain';
const currencyName = 'BNB';
const currencySymbol = 'BNB';
const rpcUrl = 'https://bsc-dataseed.binance.org/';
const blockExplorerUrl = 'https://bscscan.com';
// @ts-ignore
await Moralis.addNetwork(chainId, chainName, currencyName, currencySymbol, rpcUrl, blockExplorerUrl);
// @ts-ignore
await Moralis.switchNetwork('0x38');
}
const sessionToken = session.getSessionToken();
const bonfireBalance = await getTokenBalance(user, [bonfire_address[usedNet]]);
const bnbBalance = await getNativeBalance(user);
const user_wallet_address = user.get('ethAddress');
const me = await getCurrentUserFromAWS(user_wallet_address, { sessionToken: sessionToken, address: user_wallet_address, id: user.id });
dispatch(exeLogin(user, sessionToken, me, { [bonfire_address[usedNet]]: bonfireBalance, [bnb_address]: { balance: bnbBalance } }, provider));
}
};
where in that code did you try to add that Moralis.Web3API.account.getNFTsForContract(options)
?
That function Moralis.Web3API.account.getNFTsForContract(options)
is in another react component, which is mounted after the init functions that I wrote have been called.
I have also tried adding the startMoralis function like this immediately before the call:
await startMoralis(loginInfo);
// @ts-ignore
await Moralis.enableWeb3();
const NFTOwners = await Moralis.Web3API.account.getNFTsForContract(options);
but it is not working and still throws the same error
I tested now on vanilla js with version 0.0.183 and it works for me
you can check the loaded version for Moralis SDK this way: Moralis.CoreManager.get("VERSION")
I used this code:
x = await Moralis.Web3API.account.getNFTsForContract({
chain: 'bsc',
address: 'address'.toLowerCase(),
token_address: 'token_address'.toLowerCase()
})
Hey sorry, I’m pretty confused now… The code works without problems. I didn’t make any changes and now the call does not throw that error anymore.
I hope a random error like this will not happen again though. I’m not sure what might have caused it