2

I have 2 functions in blockchain contract to get the investors and funds for an address and tried to save that in excel.

1st Function To get the list of investors

getInvestorsList()

2nd Function This will take the investor address as input and returns the investors address and the funds saved for that address

getInvestorsAndBalances(address investorAddress)

I was able to get the list of investors and the finds sponsored by them using the functions "getInvestorsList" and "getInvestorsAndBalances".

Question

  1. The below snippet which convert the data to excel has to executed only when the function "getInvestorsAndBalances" executes completely for all investors. But this code is executed even before the call to the contract completes. Hence I am not getting the values from the blockchain to the below snippet.

How to make the below code wait for the successful completion of the function "getInvestorsAndBalances" ?

                dataSample = dataSample + "]";
                console.log("dataSample: " + dataSample);
                //var dataSample = [{"address": "abc","balance": "21.22"}]; 
                const xls = new XlsExport(dataSample,  'Example WB');  
                xls.exportToXLS('export.xls')  

Complete Code

    crowdSaleContractObj.getInvestorsList(function(error, result){
            if(!error)
                {    
                    for (i=0; i < result.length; i++) {  

                        crowdSaleContractObj.getInvestorsAndBalances(result[i],function(error, result1){
                        console.log(i);

                        if(!error)
                            {      
                                console.log(i + " - Address : " + result1[0]+ ",  Balance : " + result1[1]);
                                element = " {\"address\": " + result1[0] + ",balance:" + result1[1] + "},";
                                console.log("element: " + element);
                                dataSample = dataSample + element;
                            }
                        else
                            console.error(error);
                        });   
                    }

                    dataSample = dataSample + "]";
                    console.log("dataSample: " + dataSample);
                    //var dataSample = [{"address": "abc","balance": "21.22"}]; 
                    const xls = new XlsExport(dataSample,  'Example WB');  
                    xls.exportToXLS('export.xls')  

                }
            else
                console.error(error);
    });  
1

The loop is iterating regardless of the response of contract call and executes the code

 dataSample = dataSample + "]";
console.log("dataSample: " + dataSample);
//var dataSample = [{"address": "abc","balance": "21.22"}]; 
const xls = new XlsExport(dataSample,  'Example WB');  
xls.exportToXLS('export.xls')  

You can use async/await to make the code wait. It will look something like this

crowdSaleContractObj.getInvestorsList(async function(error, result){
            if(!error)
                {    
                    for (i=0; i < result.length; i++) {  

                        var result1 = await crowdSaleContractObj.getInvestorsAndBalances(result[i])
                        console.log(i);     
                        console.log(i + " - Address : " + result1[0]+ ",  Balance : " + result1[1]);
                        element = " {\"address\": " + result1[0] + ",balance:" + result1[1] + "},";
                        console.log("element: " + element);
                        dataSample = dataSample + element;  
                    }

                    dataSample = dataSample + "]";
                    console.log("dataSample: " + dataSample);
                    //var dataSample = [{"address": "abc","balance": "21.22"}]; 
                    const xls = new XlsExport(dataSample,  'Example WB');  
                    xls.exportToXLS('export.xls')  

                }
            else
                console.error(error);
    });

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.