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

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.

  • 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 – user1351757 May 6 '18 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?

  • 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 – user1351757 May 7 '18 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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