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');
} ```
CodePudding user response:
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