Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: Cannot read properties of undefined (reading '0'). Terminal hit this error after deployment of script

I get such a problem in my code. I fight and fight, I still haven’t found an error 🙁

Terminal after call this script hit " TypeError: Cannot read properties of undefined (reading ‘0’)"


  const nft = "cardienft.json";

  
  let rares = JSON.parse(fs.readFileSync(nft));
  let nonce = 73;
  for(let i = 0; i < 2000; i += 25) {
      let moonies = [];
      for(let j = 0; j < 25; j++) {
        moonies.push({ uri: rares.doggies[i+j].uri, moonieType: 6 });
      }
      console.log(await Gateway.forgeRares(moonies, { gasLimit: 10000000, gasPrice: 2000000000, nonce: nonce }));
      nonce++;
      console.log(i + ' to ' + (i+25) + ' forged');
  } ```

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

when your code arrives at this line :

moonies.push({ uri: rares.doggies[i+j].uri, moonieType: 6 });

rares is an empty object , so rares.doggies is undefined

i and j are 0 and i+j = 0
it want to read 0 key from undefined(rares.doggies).

you can log rares before starting for loop to get sure and then handle its value

or you can add a ? between rares.doggies and [i+j] like this:

moonies.push({ uri: rares.doggies?.[i+j].uri, moonieType: 6 });

as a trick

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading