Web3异步/等待问题

时间:2018-07-18 18:29:13

标签: async-await ethereum web3js

我正在开发一个简单的以太坊项目,但是由于我不确定我遇到的问题是与Web3还是Async / Await有关,因此我决定将此问题同时发布到Stackoverflow和以太坊Stackexchange。我正在将javascript与Web3一起用于此项目。

本质上,我正在运行三个异步函数,其中一个用于在执行一些计算并返回其工作结果之前调用其他函数:

async function getFounder() {
    console.log("getFounder method");
        try{
           console.log("just before whoIsFounder");
           whoIsFounder = await promisify(RegionalContract2.getFounder);
        }catch(err) {
           console.log(err)
        }

            console.log("This Election's Founder is : " + whoIsFounder);

        return whoIsFounder;        

    }

async function getCurrentAccount(){   <<<<<<The problem method
console.log("getCurrentAccount method");
currentAccount = await web3.eth.getAccounts();    
return currentAccount;
}

async function isItYours(){
   try{
    console.log("Inside the isItYours method");
        currentAccount = await getCurrentAccount();
        founder = await getFounder();
        console.log("The getFounder method has resolved completely");
        console.log("founder is " + founder);
      if (founder != null){
            if (founder === currentAccount) {//if the Founder is the same as the value entered.
            alert("You are in fact the owner of this Election.");
            var x = document.getElementById("hidden");
            x.style.display = "block";
            }
            else {
            alert("You are NOT the owner of this Election. The value sent in was " + currentAccount);
            }
      }
      else {
        alert("Founder can't be null. Either you're not logged into Metamask, or your account somehow isn't linked up properly.");
      }
     }catch (err) {
        console.log(err)
     }
}
isItYours();

但是,当我尝试运行此代码时,出现以下错误:

Uncaught TypeError: e is not a function
    at inpage.js:1
    at inpage.js:1
    at inpage.js:1
    at inpage.js:1
    at i (inpage.js:1)
    at inpage.js:1
    at inpage.js:1
    at u (inpage.js:1)
    at s (inpage.js:1)
    at inpage.js:1

完整的控制台日志是:

The founder is:
inpage.js:1 null "0x59a09d00eb11caa5fa70484bf4d78015d10938bc"
admin.html:340 Inside the isItYours method
admin.html:331 getCurrentAccount method
admin.html:305 getFounder method
admin.html:318 just before whoIsFounder
***the aforementioned error from the previous segment***
This Election's Founder is : 0x59a09d00eb11caa5fa70484bf4d78015d10938bc
admin.html:344 The getFounder method has resolved completely
admin.html:345 founder is 0x59a09d00eb11caa5fa70484bf4d78015d10938bc

这使我认为问题出在getCurrentAccount()上,因为它使用web3.eth.getAccounts()而不是自定义方法来返回其值。有谁知道为什么会这样?谢谢。

0 个答案:

没有答案
相关问题