我在区块链合同中有两个功能来获取地址的投资者和资金,并试图将其保存在Excel中。
第一个功能:获取投资者列表:
getInvestorsList()
第二个功能:它将以投资者地址为输入,并返回投资者地址和为该地址节省的资金:
getInvestorsAndBalances(address investorAddress)
我可以使用“ getInvestorsList”和“ getInvestorsAndBalances”功能获取投资者名单和他们赞助的资金。
问题
如何使以下代码等待功能“ 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')
完整代码
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);
});
答案 0 :(得分:2)
这是您的代码的有效版本:
crowdSaleContractObj.getInvestorsList(function(error, results) {
if (!error) {
const promises = results.map(function(result, i) {
return new Promise((resolve, reject) => {
crowdSaleContractObj.getInvestorsAndBalances(result[i], function(
error,
result1
) {
console.log(i);
if (!error) {
console.log(
i + " - Address : " + result1[0] + ", Balance : " + result1[1]
);
resolve(result1);
} else {
console.error(error);
reject(error);
}
});
});
});
Promise.all(promises)
.then(function(results1) {
results1.forEach(r => {
element = ' {"address": ' + r[0] + ",balance:" + r[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");
})
.catch(function(error) {
console.error(error);
});
} else console.error(error);
});
for
循环将结果转换为promises的数组,该数组解析为返回到getInvestorsAndBalances
中的回调的值then
方法中的代码仅在异步检索了所有投资者和余额之后才执行改进代码
我已经自由地重构了您的代码,以使用现代语言功能并修复了一些错误:
crowdSaleContractObj.getInvestorsList((error, results) => {
if (error) {
console.error(error);
return;
}
const promises = results.map(
(result, i) =>
new Promise((resolve, reject) => {
crowdSaleContractObj.getInvestorsAndBalances(
result[i],
(error, result1) => {
if (error) {
reject(error);
return;
}
resolve(result1);
}
);
})
);
Promise.all(promises)
.then(results1 => {
const dataSample = `[${results1
.map(r => `{"address": "${r[0]}", "balance": ${r[1]}}`)
.join(", ")}]`;
const xls = new XlsExport(dataSample, "Example WB");
xls.exportToXLS("export.xls");
})
.catch(function(error) {
return console.error(error);
});
});
dataSample
;您根本没有声明它,i
或element
都没有,这使它们成为全局变量–这是一个不好的习惯dataSample
字符串,并与Array.join结合使用以避免出现在数据末尾的逗号结尾的问题(导致JSON格式无效)