[Solved]1155 Get NFTs by contract get null token uri

Hi Allļ¼Œ
I try to use Get NFTs by contract API to get token and I write return uri like this

mapping(uint256 => string) public tokenURI;
...
...
 function uri(uint256 _id) public override view returns (string memory) { 
    return tokenURI[_id];
  } 

But I always get a null token uri, but I can get correct uri from remix.
it seems like the moralis canā€™t get the mapping type value.

what is the uri that is returned for a token id?

does it have 64 leading zeros or is a completely different format?

I want to put string to determine the nft box if is opened.
I try to put simple string in tokernURI like ā€œopendā€ļ¼Œ
or http url string like ā€œhttps://ipfs.io/ipfs/bafybeihjjkwdrxxjnuwevlqtqmh3iegcadc32sio4wmo7bv2gbf34qs34aā€.
No matter what I put in , the return is null . I also try to return

 function uri(uint256 _id) public override view returns (string memory) { 
    return string(abi.encodePacked(
            tokenURI[_id],
            Strings.toString(_tokenId),
            ".json")
           )
  } 

it only return the second half part like ā€œ1.jsonā€, the tokenURI[_id] seems always return null.
But it still worked well on remix .

what is the chain, contract address and token id where you are testing?

i am testing on goerli testnetļ¼ŒtokenID I tested only like 1,2,3 ā€¦ no padding

are you sure that you want to use erc1155?
isnā€™t erc721 better for your use case?

I think what the 721 can do, the 1155 can do either, and 1155 is more powerful .

I have deploied to goerli 0xaCA8E8D4982424048e2d0A446ffC5761a356fbf6

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";

contract MyToken is ERC1155, Ownable {

    string public name;
    string public symbol;
    mapping(uint256 => string) public tokenURI;

    constructor() ERC1155("") {
       name = "Crosschain Rally";
       symbol = "XCARS";
    }

    function setURI(string memory newuri) public onlyOwner {
   
        _setURI(newuri);
    }

    function mint(
        address account,
        uint256 id,
        uint256 amount,
        bytes memory data
    ) public onlyOwner {
        _mint(account, id, amount, data);
    }

    function mintBatch(
        address to,
        uint256[] memory ids,
        uint256[] memory amounts,
        bytes memory data
    ) public onlyOwner {
        _mintBatch(to, ids, amounts, data);
    }

    function setTokenURI(uint256 _id, string memory newuri) external   {
        tokenURI[_id] = newuri;
        emit URI(newuri, _id);
    }

    function uri(uint256 _id) public view override returns (string memory) {
        
         return string(abi.encodePacked(tokenURI[_id],Strings.toString(_id), ".json"));
    }
 
}

Done. I found I need to call Resync after update uri everytime.

1 Like