Trouble accessing NFT metadata from smart contract

Hey everyone, I’m stuck trying to fetch metadata for my Genesis NFTs from the DriverzNFT contract. I wrote a script to grab the info, but it’s not working as expected. Here’s a simplified version of what I’m trying:

pub fun main(address: Address): [&NFT] {
  let collection = getAccount(address).getCapability(CollectionPublicPath)
    .borrow<&Collection{Public, Receiver, ResolverCollection}>()
    ?? panic("Collection not found")

  let nfts: [&NFT] = []
  for id in collection.getIDs() {
    nfts.append(collection.borrowNFT(id: id))
  }
  return nfts
}

I’ve double-checked the transaction for NFT storage and linking, and I’ve looked at how the collection interface is set up in the contract. I’m stumped on why I can’t pull this data.

Interestingly, when I run a separate script to check if the NFT exists in my account, it comes back positive. So the NFT is there, but I can’t access its metadata. Am I missing something obvious? Any help would be awesome!

yo nina, have u tried checkin the contract’s events? sometimes metadata gets emitted there. also, make sure ur using the right capability for borrowin NFTs. could be a permissions thing.

maybe try printin out each step to see where it’s failing. like:

log(collection.getIDs())
log(collection.borrowNFT(id: id))

that might help narrow down the issue. good luck!

hey there, nina! i totally get your frustration with nft metadata. it can be such a pain sometimes :sweat_smile:

have you considered that maybe the issue is with how you’re accessing the metadata after borrowing the nft? instead of just returning the borrowed references, you might wanna try explicitly accessing specific metadata fields.

what if you tweaked your script to something like this:

pub fun main(address: Address): [AnyStruct] {
  let collection = getAccount(address).getCapability(CollectionPublicPath)
    .borrow<&{CollectionPublic}>()
    ?? panic("oops, collection not found!")

  let metadata: [AnyStruct] = []
  for id in collection.getIDs() {
    let nft = collection.borrowNFT(id: id)
    metadata.append(nft.getMetadata())
  }
  return metadata
}

this assumes your nft has a ‘getMetadata()’ function or something similar. you might need to adjust based on how your contract is set up.

also, have you double-checked that your nft implements all the necessary interfaces for metadata access? sometimes that can trip things up.

let me know if this helps or if you need more ideas! good luck with your project :raised_hands:

I encountered a similar issue when working with NFT metadata retrieval. The problem might lie in how you’re accessing the NFT’s metadata after borrowing it. Instead of just returning the borrowed NFT references, try explicitly accessing the metadata fields you need.

Consider modifying your script to something like this:

pub fun main(address: Address): [AnyStruct] {
  let collection = getAccount(address).getCapability(CollectionPublicPath)
    .borrow<&{CollectionPublic}>()
    ?? panic("Collection not found")

  let metadata: [AnyStruct] = []
  for id in collection.getIDs() {
    let nft = collection.borrowNFT(id: id)
    metadata.append(nft.metadata)
  }
  return metadata
}

This assumes your NFT has a ‘metadata’ field. Adjust accordingly based on your specific contract structure. Also, ensure you’re using the correct capability and that your NFT implements the necessary interfaces for metadata access.