How can my token be nonexistent?

I keep running into this error with ERC721URIStorage

Error: VM Exception while processing transaction: reverted with reason string 'ERC721URIStorage: URI query for nonexistent token'

From what I understand as along as my URL returns valid JSON this should all work smoothly. I've seen a few examples that call the increment() method at different times but other than that I'm not sure what I'm doing wrong.

If _safeMint() is successful, why would _setTokenURI() fail? I'm very new to Solidity and web3 in general so I'm assuming nothing.

:1234: Code to reproduce

pragma solidity 0.8.4;

import "hardhat/console.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";

contract MyNFT is ERC721URIStorage, Ownable {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor() ERC721("MyNFT", "MNFT") {}

    function createToken(uint256 tokenURI) public returns (uint256) {
        _tokenIds.increment();
        uint256 newTokenID = _tokenIds.current();

        string memory uri = append(
            "https://{some_url}/api/metadata/",
            uintToString(tokenURI)
        );

        _safeMint(msg.sender, tokenURI);
        _setTokenURI(newTokenID, uri);

        return newTokenID;
    }
}

:computer: Environment

Hardhat, Hardhat Deploy, TypeScript, Node v16.5.0

You are passing the tokenURI where instead you should be passing the tokenID

1 Like