1

I am unable to add elements to a array within the struct struct D { string name; address key; } struct Aone { string aid; B[] blist; D[] dlist; } struct B{ string bdesc; bool bstatus; C[] clist; }

struct C{
    uint createTime;
    string cdesc;
    bool rstatus;
    string ctype;   
}

mapping(string => Aone) A;

I am need to add items to struct c based on aid provided function rptC(string _aid, string _c) public { B[] storage bhist = A[_aid].blist;

        if(bhist.length > 0 && bhist[bhist.length-1].bstatus==false) {
            bhist[bhist.length-1].clist.push(C(now,_c,false,""));
        } else if(bhist[bhist.length-1].bstatus==true) {
            bhist[bhist.length].clist.push(C(now,_c,false,""));
        } else {
            bhist[0].clist.push(C(now,_c,false,""));
        }
}

I keep getting a invalid json rpc error. I am new to Solidity and need help

Thank you

3 Answers 3

0
clist.push(c(now,_problem,false,"")

You're pushing a c (shouldn't it be a C?) struct with 4 fields, but the struct has only 3 fields.

1
  • I am sorry . When i wrote the question i missed the 4th field in struct c struct C{ uint createTime; string cdesc; bool rstatus, string ctype; } . Also it should capital C ..wrote it incorrectly as small c in the question May 6, 2018 at 22:00
0

This is difficult to answer you with this short parth of your source code but are you sure that the bhist struct already exist before adding the clist?

0

I think I found the problem:

B[] storage bhist = A[_aid].blist

You're using A has a mapping, not has a struct.

I made a few changes, and now the following code is working on remix:

pragma solidity ^0.4.23;

contract Test {

    mapping ( string => B[] ) A;

    struct B {
        string bdesc;
        bool bstatus;
        C[] clist;
    }

    struct C {
        uint createTime;
        string cdesc;
        bool rstatus;
        string ctype;   
    }

    function rptC(string _aid, string _c) public {
        B[] storage bhist = A[_aid];
        if(bhist.length > 0 && bhist[bhist.length-1].bstatus==false) {
            bhist[bhist.length-1].clist.push(C(now,_c,false,""));
        } else if(bhist[bhist.length-1].bstatus==true) {
            bhist[bhist.length].clist.push(C(now,_c,false,""));
        } else {
            bhist[0].clist.push(C(now,_c,false,""));
        }
    }
}

Does this help?

1
  • Thank you for your reply. However i am still not sure how to update the below as per your instruction I am unable to add elements to a array within the struct May 7, 2018 at 16:13

Your Answer

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

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