saeedhsb
saeedhsb

Reputation: 3

Solidity uint256[] not returning from a function

I have a function that returns an uint256[] from this mapping : mapping(address => uint256[]) ownerToIds

but the problem is that when I try to return the array elements it does nothing.

I already tried adding public and view to function but it didn't change anything.

code: (using remix in binance testnet)

mapping(address => uint256[]) ownerToIds;

function getOwnersOwnedTokenIds(address owner) public returns(uint256[] memory){
    return ownerToIds[owner];
}

Upvotes: 0

Views: 406

Answers (1)

Antonio Carito
Antonio Carito

Reputation: 1387

When you want return a specific data for handle it outsite your smart contract, you must use view access modifier. It allows you to extrapolated data from your smart contract.

In your case, you can try to modify your getOwnersOwnedTokenIds() function in this way:

function getOwnersOwnedTokenIds(address owner) public view returns(uint256[] memory){
  return ownerToIds[owner];
}

Example of a working smart contract:

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

contract Test {

    mapping(address => uint256[]) ownerToIds;

    constructor() {
        ownerToIds[msg.sender].push(1);
        ownerToIds[msg.sender].push(2);
        ownerToIds[msg.sender].push(3);
        ownerToIds[msg.sender].push(4); 
    }

    function getOwnersOwnedTokenIds(address owner) public view returns(uint256[] memory){
        return ownerToIds[owner];
    }

}

Upvotes: 1

Related Questions