我有以下功能。我试图逐行读取csv文件,并尝试同时获取帐户详细信息,如下所示。

async function Upload() {
            var fileUpload = document.getElementById("fileUpload");
            var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.csv|.txt)$/;
            if (regex.test(fileUpload.value.toLowerCase())) {
                if (typeof (FileReader) != "undefined") {
                    var reader = new FileReader();
                    reader.onload = function(e) {
                        var csv = e.target.result;
                        var data = $.csv.toObjects(csv);
                        var investorsBalance = "";
                        for(var i=0;i<data.length;i++){
                            investorsBalance = await getAccountDetailsInvestor(data[i].investor_id,data[i].asset_code).then(ret_val => {
                                alert("ret_val :"+ret_val);
                                investorsBalance = ret_val;
                            })
                            alert("investorsBalance :"+investorsBalance)
                        }
                    }
                    reader.readAsText(fileUpload.files[0]);
                } else {
                    alert("This browser does not support HTML5.");
                }
            } else {
                alert("Please upload a valid CSV file.");
            }
        }

async function getAccountDetailsInvestor(investorId,assetCode){
    var investorsBalance = "";
    console.log("getAccountDetails :"+investorId);
    $("#overlay").show();
    var accountKeys = "";
    var userLoadAccount = "";
    userLoadAccount = await server.loadAccount(investorId);
    var data = userLoadAccount.balances;
    var count = 1;
    var tempData = "";
    data.forEach(function(obj){
        var res = (obj.asset_type =="native");
        if(!res){
            if(obj.asset_code == assetCode){
                investorsBalance = obj.balance
            }
        }
    })
    $("#overlay").hide();
    return investorsBalance;
}


我遇到了错误。

Uncaught SyntaxError: await is only valid in async function


我要等到getAccountDetailsInvestor()函数完成。怎么做?

最佳答案

您需要将reader.onload = async function (e)...回调函数标记为异步

10-08 00:19