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!