JavaScript workflow for NFT contract interaction: Beginner's query

Help needed with NFT contract view function in JavaScript

I’m new to NFT development and I’m trying to switch from CLI commands to JavaScript for interacting with an NFT contract. I’ve deployed the contract to a subaccount and I’m attempting to call a view function.

Here’s what I’ve done so far:

const contract = await nearConnection.loadContract('nft-contract.myaccount.testnet', {
  viewMethods: ['nft_info', 'token_details', 'owner_tokens'],
  changeMethods: ['mint_nft', 'transfer_nft', 'approve_nft'],
  sender: walletAccount.getAccountId()
});

try {
  const metadata = await contract.nft_info();
  console.log(metadata);
} catch (error) {
  console.error('Error:', error.message);
}

When I run this, I get an error saying ‘missing field account_id’. I’m not sure why this is happening or how to fix it. Does anyone know what I’m doing wrong? Is it related to how I’m setting up the connection or calling the function?

hey there creativePainter45! i’m also pretty new to nft stuff, but i’ve been tinkering with it lately. have you double-checked that your contract actually has an ‘nft_info’ function? sometimes the error messages can be a bit misleading.

also, i’m curious - what kind of nfts are you working on? anything cool? :art:

one thing that helped me was looking at example projects on github. there’s tons of stuff out there that might give you ideas on how to structure your code.

oh, and have you tried using a different view method? maybe ‘token_details’ or ‘owner_tokens’ might work differently?

let us know how it goes! i’m really interested to see what you come up with for your nft project :blush:

yo creativePainter45, sounds like ur having a tough time. have u tried console.logging the walletAccount.getAccountId() to make sure its actually returning something? sometimes these little things trip us up. also, double check ur contract’s nft_info function. it might be expecting some params ur not giving it. keep at it, nft dev can be tricky but its worth it!

I encountered a similar issue when working with NFT contracts. The ‘missing field account_id’ error often occurs when the contract expects an account ID parameter that isn’t being provided. Try modifying your function call to include the account ID:

const metadata = await contract.nft_info({ account_id: walletAccount.getAccountId() });

This should resolve the error. If it persists, double-check your contract’s view function implementation to ensure it’s correctly handling the account_id parameter. Also, verify that your NEAR connection and contract loading are set up correctly. Debugging NFT contracts can be tricky, but perseverance pays off. Good luck with your project!