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:
Remix outputs:
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.
Kindly help me out in analyzing where I am going wrong please.
Vivek.