0

Below is the contract:

uint32 firstTimeUserPoints=10;
    struct struUser{
        string orginalName;
        string originalKshoplink;
        string orginalDocumentConvention;
        bool validated;
        uint32 Points;
        uint32 totalPoints;
    }
    struct struUserTotalPoints{
        string orginalName;
        uint32 totalPoints;
    }
    mapping(string=>struUser)MappingToken;
    mapping(bytes32=>struUser)SampleMappingToken;
    struUser[] struUsers;
    struUserTotalPoints[] struArrayUserTotalPoints;
    string[] docConventionMappers;
    string[] orginalName;

//TO INSERT A RECORD WITH SOME DATA

function AddUser(string name,string link,string docConvention) public
{
        var user=MappingToken[docConvention];
        var existingTotalPoints=userTotalPoints(name);

        user.orginalName=name;
        user.originalKshoplink=link;
        user.validated=false;
        user.Points=firstTimeUserPoints;
        user.totalPoints=existingTotalPoints+firstTimeUserPoints;
        updateUserTotalPoints(name,user.totalPoints);
        user.orginalDocumentConvention=docConvention;
        docConventionMappers.push(docConvention);

        struUsers.push(struUser({orginalName:name,originalKshoplink:link, 
        validated:user.validated,Points:user.Points,totalPoints:user.totalPoints,
        orginalDocumentConvention:docConvention}));

    }

//IS THE BELOW FUNCTION FINE?, THIS IS ONE OF THE PLACE I BELIEVE IT IS GETTING MESSED.

//FOR FETCHING TOTAL POINTS OF A USER

 function userTotalPoints(string name) public returns(uint32){  

         bool isUserTotalPointsCalculated=false;

            for(uint j=0;j<struArrayUserTotalPoints.length;j++){
                if(keccak256(struArrayUserTotalPoints[j].orginalName)==keccak256(name)){                    
                    isUserTotalPointsCalculated=true;
                    return struArrayUserTotalPoints[j].totalPoints;
                }
            }
            if(!isUserTotalPointsCalculated){
                struArrayUserTotalPoints.push(struUserTotalPoints({orginalName:name,totalPoints:firstTimeUserPoints}));
                return 0;                
            }  
    }
//WHEN I CALL THE BELOW METHOD IT IS RUNNING FINE IN REMIX BUT NOT WHEN I USED WITH TRUFFLE - IN TRUFFLE THE STRING IS  RETURNED AS " " AND THE TOTAL POINTS 

    function viewuser(string docConvention)public returns(string,uint32,uint32)
{ 
        return (MappingToken[docConvention].orginalName,MappingToken[docConvention].Points,
        userTotalPoints(MappingToken[docConvention].orginalName));
    }

    function updateUserTotalPoints(string name,uint32 totalPoints) public{
        for(uint j=0;j<struArrayUserTotalPoints.length;j++){
                if(keccak256(struArrayUserTotalPoints[j].orginalName)==keccak256(name)){
                    struArrayUserTotalPoints[j].totalPoints=totalPoints;
                }
            }
    }

I have tried for different inputs and it is working fine.

Remix inputs:

enter image description here

Remix outputs:

enter image description here

HTML:

<input id="txtViewUser" type="text" placeholder="Enter name of document convention" />
<button id="btnViewUser" onclick="App.viewUser()">View User</button>

Truffle code:

viewUser:function () {
    var docConvention=document.getElementById("txtViewUser");
    sampleinstance.deployed().then(function (instance) {      
        return instance.viewuser.call(docConvention).then(function (v) {
        console.log(v);
        $("#tdEmailName").text(v[0].toString());
        $("#tdPoints").text(v[1].toNumber());
        $("#tdTotalPoints").text(v[2].toNumber());
      })
    })
  },

Truffle Output:

the 0 array will return the name of the user

I have inserted different users five times so it is giving 50 where I have tried to view user for one person.

enter image description here

Kindly help me out in analyzing where I am going wrong please.

Vivek.

1 Answer 1

0

It looks like it may be working as expected but you did not understand to expect BigNumber format. This is decoded for you by Remix.

BigNumber is a way to represent a number that has more significant digits than JS can handle (uint256).

Try converting your function responses to a readable format.

console.log(response.toString(10));

If that is the right answer, then consider reading up on BigNumber library that is included in Web3.

Hope it helps.

3
  • Sorry for the delay in responding. Tried - console.log(v.toString(30)); but still no luck. output is something like " ,10,20". Still i am missing the string value. How do i get the string output? May 8, 2018 at 6:46
  • v.toString(10); May 8, 2018 at 14:24
  • even tried v.toString(10) earlier still it's the same. May 9, 2018 at 7:02

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.