简体   繁体   English

在 JS 循环内调用以太坊智能合约函数不同步

[英]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.如果我通过 javascript 调用它,它工作得很好,但我有一种情况,我必须在 for 循环下的 javascript 内部调用这个函数。 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.问题是当我将这个参数提供给paperContractObj.getPaperValues时它是“index”的参数,它从区块链返回正确的值,但参数“Index”值被改变了。 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.如何确保此合约函数中的参数不会因 for 循环而更改。

It is going to call asynchronously the contract function.它将异步调用合约函数。

Just put就放

await等待

before your function call在你的函数调用之前

paperContractObj.getPaperValues 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.即使在合约函数调用的基于事件的结果中,使用 const 也会给出正确的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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