FirstDapp tutorials from 21st May

in App.js at async function getRatio , at variable definition let up = Number(results.attributes.up); VsCode does not throw any error but in my console Iam getting uncaugh in promise error cannot read property of undefined( reading attributes ) , i cleared my code and used yours on Github trying to fix it but i get the same error . How to fix it ? It makes my app freeze( no up or down or connect wallet button is working ) but votes update when i vote on etherscan

can you post more code close to that line?
can you add some logging with console.log to see what happens?

here is again the description of the error in the console

the link to the code on Moralis github Repo , error occur at line 29

it looks like this is the function:

async function getRatio(tick, setPerc){

    const Votes = Moralis.Object.extend("Votes");
    const query = new Moralis.Query(Votes);
    query.equalTo("ticker", tick);
    query.descending("createdAt");
    const results = await query.first();
    let up = Number(results.attributes.up);
    let down = Number(results.attributes.down);
    let ratio = Math.round(up/(up+down)*100);
    setPerc(ratio);
  }

here it tries to query the database for Votes table, for ticker tick that is received as parameter.

most probably it doesn’t find anything in the database and that it why it gives that error.
you can add an if for that result from this line const results = await query.first();, if result is undefined then not to run the next lines

thanks you sir i wrapped the next lines in an if and it works

code to fix
if(results){

let up =  Number(results.attributes.up);

let down =  Number(results.attributes.down);

let ratio = Math.round(up/(up+down)*100);

setPerc(ratio);

}

Is there sometyhing i can do to thanks you as increase your reputation on the forum or an upvote ? I was trying to solve it since 2 hours now ; lol thanks

1 Like