简体   繁体   中英

Calling Ethereum Smart contract function inside a JS loop is not synchronize

I have a smart contract which has the function returns some values based on the stored data. The function is defiend as below

 function getPaperValues(uint _paperId) public view returns(string memory title,string memory ipfscid,address author,PaperStatus status,address[] memory reviewers,bytes32[] memory keywords,uint version)
{
    return (paperFiles[_paperId].title,paperFiles[_paperId].ipfs_cid,paperFiles[_paperId].author,paperFiles[_paperId].status,paperFiles[_paperId].reviewers,paperFiles[_paperId].keywords, paperFiles[_paperId].version_number);
}

It works perfectly fine if I call it through javascript but I have a situation where I have to call this function inside the javascript under a for loop. The problem is the parameter which is "index" when I provide this to paperContractObj.getPaperValues it returns me correct values from the blockchain but the parameter "Index" value gets changed. It seems like the function is not properly synchronized with its call time and result time.

                for (loop = 0; loop < result.length-1; loop++)
                {

                    var index = result[loop];
                    // this function takes index and return correct values as per associated index
                    paperContractObj.getPaperValues(index,{from:account},function (err, result2) {

                    // On this line index value shows different values even though result I received inside result2 is associated with the correct index.  

                    console.log("loop: -"+index); //need to fix this part loop id not coming properly it shows 

                        if (err) {
                            console.log("CALL-ERROR-REASON: "+JSON.stringify(err));
                            console.log("REASON: Unable to retrieve Paper values by Ids");
                        }
                        else {
                            console.log("CALL-SUCCESS: "+JSON.stringify(result2));
                            console.log("CALL-ACHIEVE: Recieved current papers values via ids successfully"+result[loop]);
                            addListHtmlDivs(result2,index); 
                            // Here values from the chain is coming fine but index which was provided as a parameter to this function is changed or different

                        }
     });

                }

How can I make sure the parameter inside this contract function do not get changed due to the for loop.

It is going to call asynchronously the contract function.

Just put

await

before your function call

paperContractObj.getPaperValues

The problem is solved using a constant index within the loop

for (loop = 0; loop < result.length-1; loop++)
{

                    const index = result[loop];

using const will give the correct value even inside the event-based result of contract function call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM