Please it is not spam! i need help with the moralis dex course web3api

Hello Ivan, i am having trouble with the javascript programming course where we create a DEX using moralis.io. i keep getting this promise error message. please help.

This is the error i receive on Inspect element on google chrome browser:
Uncaught (in promise) Error: Web3Api not initialized, run Moralis.start() first
at Function. (moralis.js:7005)
at tryCatch (moralis.js:28071)
at Generator.invoke [as _invoke] (moralis.js:28301)
at Generator.next (moralis.js:28126)
at asyncGeneratorStep (moralis.js:27580)
at _next (moralis.js:27602)
at moralis.js:27609
at new Promise ()
at new Wrapper (moralis.js:32483)
at Function. (moralis.js:27598)

and this is the message i get on visual code studio:
Property ‘Web3API’ does not exist on type ‘typeof Moralis’.ts(2339)

1 Like

Hey @dsavv, hope you are well.

Could you please provide the code from your index.js file in the following way?

Carlos Z

Thank you for your response. Here is my code:

// connect to Moralis server

Moralis.initialize("lnU3OKs3YT2L0QCKx0mTZN6x9ZcMZPRSpY9uATrW");

Moralis.serverURL = "https://hxhws5m4111t.usemoralis.com:2053/server";

// add from here down

async function login() {

    let user = Moralis.User.current();

    if (!user) {

        user = await Moralis.authenticate();

    }

    console.log("logged in user:", user);

    getStats();

}

async function getStats() {

    const balances = await Moralis.Web3API.account.getTokenBalances({chain: 'polygon'});

    console.log(balances);

}

async function logOut() {

    await Moralis.User.logOut();

    console.log("logged out")

}

document.querySelector("#btn-login").addEventListener('click', login);

document.getElementById("btn-logout").addEventListener('click', logOut);

 

async function getTop10Tokens() {

    const response = await fetch('https://api.coinpaprika.com/v1/coins');

    const tokens = await response.json();

    return tokens  

            .filter(token => token.rank >= 1 && token.rank <= 30 )

            .map(token => token.symbol);

}

async function getTickerData(tickerList) {

    const response = await fetch('https://api.1inch.exchange/v3.0/137/tokens');

    const tokens = await response.json();

    const tokenList = Object.values(tokens.tokens);

    return tokenList.filter(token => tickerList.includes(token.symbol));

}

getTop10Tokens()

    .then(getTickerData)

    .then(console.log);
1 Like

Ok, so what you are just missing is to initialize the moralis server.

the new method is the following:

	// initialize moralis server
	const serverUrl = "URL SERVER HERE";
	const appId = "SECRET APP KEY HERE";
	Moralis.start({ serverUrl, appId });

Hope it helps.

Carlos Z

This worked with initialising Web3. However, a new error has occured with the async function:

1 Like

i believe i managed to solve it but now there is another unexepcted token erro. i think there is an issue within the way ive written the code.

1 Like

1 Like

Ok, from what I see in your images, is a syntax problem.

In function header: async function :getStats(), there is a : sign which should not be there. take a look into the logOut function for example.

also at const :balance, and at the end of this declaration, there is a extra , that should also not be there.

Note: Please share your future code in the following way so i can easily copy/past for quick reviews :nerd_face:
FAQ - How to post code in the forum

Carlos Z

Here is my code. it says there is 9+ errors.

// initialize moralis server

const serverUrl = "https://hxhws5m4111t.usemoralis.com:2053/server";

const appId = "lnU3OKs3YT2L0QCKx0mTZN6x9ZcMZPRSpY9uATrW";

Moralis.start({ https://hxhws5m4111t.usemoralis.com:2053/server, lnU3OKs3YT2L0QCKx0mTZN6x9ZcMZPRSpY9uATrW });

// add from here down

async function login() {

    let user = Moralis.User.current();

    if (!user) {

        user = await Moralis.authenticate();

    }

    console.log("logged in user:", user);

    getStats();

}

async function getStats() {

    const balances = await Moralis.Web3API.account.getTokenBalances({chain: 'polygon'});

    console.log(balances);

   

}

async function logOut () {

        await Moralis.User.logOut();

        console.log("logged out");

    }

document.querySelector("#btn-login").addEventListener('click', login);

document.getElementById("btn-logout").addEventListener('click', logOut);

 

async function getTop10Tokens() {

    const response = await fetch('https://api.coinpaprika.com/v1/coins');

    const tokens = await response.json();

    return tokens  

            .filter(token => token.rank >= 1 && token.rank <= 30 )

            .map(token => token.symbol);

}

async function getTickerData(tickerList) {

    const response = await fetch('https://api.1inch.exchange/v3.0/137/tokens');

    const tokens = await response.json();

    const tokenList = Object.values(tokens.tokens);

    return tokenList.filter(token => tickerList.includes(token.symbol));

}

getTop10Tokens()

    .then(getTickerData)

    .then(console.log);

I placed this : sign because when i looked at the errors, it said to include : to solve it.