JSON.parse -- TypeError: Cannot read properties of null (reading 'attributes')

Here is the code:

When I use
let metadata = allNFTs.map((e) => JSON.parse(e.metadata).attributes);
in the code I got
"TypeError: Cannot read properties of null (reading 'attributes')"

but If I use
let metadata = allNFTs.map((e) => JSON.parse(e.metadata));

I got this metadata ;

{
  name: 'Bro #1999',
  dna: 'edff418af9ddb532061ffd41317244b5bdecae65',
  external_url: 'https://avaxbros.com',
  description: '1 of 2000 randomly generated and unique characters in the AVAX Bros collection.',
  image: 'https://ipfs.io/ipfs/QmbQsNzqevJrMFHR9xy8wk2Q7gYPppD3RsVwXmUo4asP9s',
  edition: 1999,
  attributes: [
    { trait_type: 'Background', value: "Green 'C'" },
    { trait_type: 'Body', value: "Red 'C'" },
    { trait_type: 'Eyes', value: "Sleepy 'C'" },
    { trait_type: 'Hat', value: "Explorer 'R'" },
    { trait_type: 'Mouth', value: "Tongue Out 'U'" },
    { trait_type: 'Neck', value: "None 'C'" }
  ]
}

I need to get attributes to use the data. How can I fix this ?

Thank you allready.

Sorry I don’t understand. If you’ve got that metadata stored like that, you can map over it to select/display the attributes.

how can I stored it and map over it to select/display the attributes?


const Moralis = require('moralis/node');
  
const serverUrl = "https://eprfpy1pddiw.usemoralis.com:2053/server";
const appId  = "LnyEdW1E5FQ1U7bibkVRKL8xi4ohvfrKXdxEIDuk";
Moralis.start({serverUrl, appId});

// Hopper "0x4dB152A1ffb71918EAF10c0BAebD5dA535a51932"
// Fish "0x8372530bc1c3ef2739213059b113c76e143bc25d"
// Avax Big Apes


const collectionAddress = "0x21a88eb452ad36b6024b29e5518fc33342efb9da";
const collectionname = "Hopper";
const chainId = "0xa86a";



async function generateRarity(){
    
    const NFTs = await Moralis.Web3API.token.getAllTokenIds({address:collectionAddress, chain:chainId,});
    console.log(NFTs);

    const totalNum = NFTs.total; 
    const pageSize = NFTs.page_size; 
    console.log(totalNum);
    console.log(pageSize);
    let allNFTs = NFTs.result;

    const timer = ms => new Promise(res => setTimeout(res, ms))

    for (let i = pageSize; i < totalNum; i = i + pageSize) {
        const NFTs = await Moralis.Web3API.token.getAllTokenIds({
          address: collectionAddress,
          chain:chainId,
          offset: i,
        });
        allNFTs = allNFTs.concat(NFTs.result);
        await timer(6000);
      }
 
      let metadata = allNFTs.map((e) => JSON.parse(e.metadata));
      

    let tally = {"TraitCount":{}};
    for (let j = 0; j < metadata.length; j++) {
        let nftTraits = metadata[j].map((e) => e.trait_type);
        let nftValues = metadata[j].map((e) => e.value);

        let numOfTraits = nftTraits.length;

        if (tally.TraitCount [numOfTraits]) {
            tally.TraitCount [numOfTraits]++;
        } else {
            tally.TraitCount[numOfTraits] = 1;
        }
    }
        console.log(tally);

};

generateRarity();

replace
let metadata = allNFTs.map((e) => JSON.parse(e.metadata).attributes);

with

let collection = allNFTs.map(e => {
return {
metadata: JSON.parse(e.metadata),
token_id: e.token_id
};
});

let metadata = collection.map(e => e.metadata);
console.log(metadata.length);

for (let i = 0; i < metadata.length; i++) {
console.log(metadata[i]?.attributes, i);
}

2 Likes

absolute life saver thank you!