JavaScript workflow for NEAR NFT example: CLI to JS conversion help needed

Hey everyone! I’m having some trouble moving from CLI to JavaScript for the NEAR zero to hero NFT example. I’ve got it working with CLI commands, but I’m stuck on the JS part.

I’m trying to call a view function on an NFT contract that’s on a subaccount. Here’s what I’ve done so far:

this.nft_contract = await this._near.near.loadContract(this.nft_contract_id, {
  viewMethods: ['nft_metadata', 'nft_token', 'nft_supply_for_owner'],
  changeMethods: ['new_default_meta', 'nft_mint', 'nft_transfer'],
  sender: window.walletConnection.getAccountId()
});

await this.nft_contract.nft_metadata()

But I’m getting an error saying ‘missing field account_id’. The CLI example uses an account_id argument, but I can’t find that in the JS examples I’ve seen.

Am I missing something with how I’m logging in or setting up the contract? Any help would be awesome!

I’ve encountered a similar issue when working with NEAR NFTs in JavaScript. The problem likely stems from how you’re initializing the contract. Instead of using loadContract, try using the Contract class from near-api-js. Here’s a snippet that might help:

import { Contract } from 'near-api-js';

const contract = new Contract(
  window.walletConnection.account(),
  nftContractId,
  {
    viewMethods: ['nft_metadata', 'nft_token', 'nft_supply_for_owner'],
    changeMethods: ['new_default_meta', 'nft_mint', 'nft_transfer']
  }
);

const metadata = await contract.nft_metadata();

This approach automatically handles the account_id for you. Make sure you’ve properly set up your wallet connection beforehand. If you’re still having issues, double-check that your nft_contract_id is correct and that you’re connected to the right network (testnet or mainnet).

hey nina! i’ve been playing around with near nfts too and it can be a bit tricky at first. have you tried using the near-api-js library? it’s super helpful for this kinda stuff

i think the issue might be how you’re setting up the contract. instead of loadContract, try something like this:

import { connect, Contract, keyStores, WalletConnection } from 'near-api-js';

// set up the connection to near
const near = await connect({
  networkId: 'testnet', // or 'mainnet' if you're ready for the big leagues!
  keyStore: new keyStores.BrowserLocalStorageKeyStore(),
  nodeUrl: 'https://rpc.testnet.near.org',
});

// create the wallet connection
const wallet = new WalletConnection(near);

// now let's set up that contract
const contract = new Contract(
  wallet.account(),
  nftContractId,
  {
    viewMethods: ['nft_metadata', 'nft_token', 'nft_supply_for_owner'],
    changeMethods: ['new_default_meta', 'nft_mint', 'nft_transfer']
  }
);

// and finally, let's call that metadata function!
const metadata = await contract.nft_metadata();
console.log(metadata);

give that a shot and see if it works better for you. oh, and make sure your nftContractId is correct - that tripped me up a couple times lol

let me know if you need any more help or if you figure it out. i’m curious to see what kind of nft project you’re working on!

hey there! i had a similar problem. try using the Contract class from near-api-js instead of loadContract. it handles the account_id automatically. heres a quick example:

const contract = new Contract(
window.walletConnection.account(),
nftContractId,
{
viewMethods: [‘nft_metadata’, ‘nft_token’],
changeMethods: [‘nft_mint’, ‘nft_transfer’]
}
);

hope this helps!