0

I can't update the values in my solidity struct. I'm using hardhat ethers.

Reading the other posts, I have tried adding a .connect(user2) for my test. I have also used storage in my .sol file.

Console.log shows the name and status remains the same even after I run the .setMemberInactive(0) function which is supposed to change the name and status

1 0
Jane
2 0
Jane

Anyone knows what have I done wrong here?

Solidity:

enum Status {
    Active,
    Inactive
}
struct Member {
    string name;
    Status status;
}
Member[] public members;
uint256 public membersCount;

function addMember(string memory _name) external {
    Member memory member = Member(_name, Status.Active);
    members.push(member);
    membersCount++;
}

// todo not working
function setMemberInactive(uint256 index) external {
    Member storage member = members[index];
    member.name = "Jeremy";
    member.status = Status.Inactive;
}

Javascript:

// todo Changing Enum Value not Working
it("should change member status", async () => {
  let member = await demoContract.members(0);
  console.log("1", member.status);
  console.log(member.name);
  let tx = await demoContract.connect(user2).setMemberInactive(0);
  await tx.wait();
  console.log("2", member.status);
  console.log(member.name);
});
2
  • Please include the code for setMemberInactive
    – Julissa DC
    Mar 16, 2022 at 14:27
  • so sorry, i missed out the setMemberInactive function. I have added that in. Thanks
    – Hendry Lim
    Mar 17, 2022 at 0:36

1 Answer 1

0

The struct values were actually updated but issue was I didn't get the updated copy of the member. I needed to call member = await demoContract.members(0); and use the updated copy.

Thus, there is no issue with my solidity code. The problem lies in the javascript test code

below is the working code

 it("should change member status", async () => {
      let member;
      member = await demoContract.members(0);
      expect(member.name).to.be.equal("Jane");
      expect(member.status).to.be.equal(0);
      await demoContract.connect(user2).setMemberInactive(0);
      member = await demoContract.members(0);
      expect(member.name).to.be.equal("Jeremy");
      expect(member.status).to.be.equal(1);
    });

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.