0

I have defined a getter function in my contract which returns value from the nested mapping(public variable). While trying to get the value using ethersjs, it is returning me undefined even after updating the nested mapping.

Contract:

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

contract Profile {
    // state variable
    mapping(address => string[3]) public address_name_imgHash_bio;

function updateName(string memory _name) external {
    address_name_imgHash_bio[msg.sender][0] = _name;
}

function updateImg(string calldata _imgHash) external {
    address_name_imgHash_bio[msg.sender][1] = _imgHash;
}

function updateBio(string calldata _bio) external {
    address_name_imgHash_bio[msg.sender][2] = _bio;
}

// getter
function getter(uint index) external view returns(string memory) {
    return address_name_imgHash_bio[msg.sender][index];
}
}

Ethersjs code:

let Profile_name

async function updateName(name) {
        const web3Modal = new Web3Modal()
        const connection = await web3Modal.connect()
        const provider = new ethers.providers.Web3Provider(connection)
        const signer = provider.getSigner()
        const Profile_contract = new ethers.Contract(ProfileAddress, Profile.abi, signer)

        await Profile_contract.updateName(name)
        Profile_name = await Profile_contract.getter(0)
        console.log(Profile_name)
    }

On calling updateName(), the transaction is successful, but Profile_name remains undefined. (I am using hardhat localhost node) I have verified ProfileAddress, Profile.abi parameters

Thanks in advance!

8
  • the contract works on remix. therefore the problem is in your script. why is Profile_name a const :/ ?
    – Majd TL
    Commented Apr 21, 2022 at 9:13
  • Yea, i guess the same. Sorry i wrote const here, have used let in main script. Can you tell how usually we call a view function using ethersjs? Commented Apr 21, 2022 at 9:22
  • docs.ethers.io/v4/api-contract.html#connecting-to-a-contract it looks okay. no idea what is the problem
    – Majd TL
    Commented Apr 21, 2022 at 9:42
  • try this please: let tx = await Profile_contract.updateName(name); console.log(tx.hash); await tx.wait(); Profile_name = await Profile_contract.getter(0); console.log(Profile_name);
    – Majd TL
    Commented Apr 21, 2022 at 10:24
  • Profile_name is showing the value in console, but it is showing undefined when used outside the updateName function. (Profile_name is defined outside the updateName function scope) Commented Apr 21, 2022 at 11:03

1 Answer 1

0

I figured out a solution, in my code I have defined Profile_name as let Profile_name

But when I tried defining it as a State variable it worked.

const[Profile_name, setProfile_name] = useState('')
async function updateName(name) {
        const web3Modal = new Web3Modal()
        const connection = await web3Modal.connect()
        const provider = new ethers.providers.Web3Provider(connection)
        const signer = provider.getSigner()
        const Profile_contract = new ethers.Contract(ProfileAddress, Profile.abi, signer)

        await Profile_contract.updateName(name)
        output = await Profile_contract.getter(0)
        setProfile_name(output) // assigning
        console.log(Profile_name)
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.