Javascript Promise.all已完成,但出现错误:Solidity函数(元掩码)的参数数无效
我目前正在从事一个使用Javascript、Async/Await和Web3(用于以太坊)的项目。我希望创建一个前端页面,能够检测用户的元掩码地址,然后在函数中使用该地址。我已经在上一页中找到了类似的内容,但我在将其翻译到另一页时遇到了麻烦。我怀疑getCurrentAccount()没有返回任何内容,从而弄乱了Promise.all希望发送的变量的数量和类型。有关守则如下:Javascript Promise.all已完成,但出现错误:Solidity函数(元掩码)的参数数无效,javascript,async-await,ethereum,web3,Javascript,Async Await,Ethereum,Web3,我目前正在从事一个使用Javascript、Async/Await和Web3(用于以太坊)的项目。我希望创建一个前端页面,能够检测用户的元掩码地址,然后在函数中使用该地址。我已经在上一页中找到了类似的内容,但我在将其翻译到另一页时遇到了麻烦。我怀疑getCurrentAccount()没有返回任何内容,从而弄乱了Promise.all希望发送的变量的数量和类型。有关守则如下: function isInList(input) { return new Promise(function(r
function isInList(input) {
return new Promise(function(resolve, reject) {
myElection.areYouInList(input, function(error, response) {
if (error) {
reject(error);
} else {
resolve(response);
}
})
});
}
function hasntVotedYet(input) {
return new Promise(function(resolve, reject) {
myElection.haveYouVotedAlready(input, function(error, response) {
if (error) {
reject(error);
} else {
resolve(response);
}
})
});
}
function isNotOver() {
return new Promise(function(resolve, reject) {
myElection.isItOver(function(error, response) {
if (error) {
reject(error)
} else {
resolve(response);
}
})
});
}
async function getCurrentAccount() {
console.log("getCurrentAccount method");
web3.eth.getAccounts((err, accounts) => {
currentAccount = accounts[0]
})
return currentAccount; //KEEP IN MIND THAT YOU NEED TO ACTUALLY BE LOGGED INTO METAMASK FOR THIS TO WORK - OTHERWISE IT'LL RETURN UNDEFINED BECAUSE THERE AREN'T ANY ACCOUNTS
}
async function verify() {
console.log("Verify");
try {
-- -- > THIS LINE //
var myCurrentAccount = await getCurrentAccount();
console.log("myCurrentAccount is: " + myCurrentAccount);
data = await Promise.all([isInList(myCurrentAccount), hasntVotedYet(myCurrentAccount), isNotOver()]); //data then equals an array, containing both the strings returned from these two methods
console.log("success");
console.log(data)
if (data[0] && !data[1] && !data[2]) { //check firstly that we are allowed to vote, secondly that we have not already voted, and finally that this vote is not over yet
var x = document.getElementById("hidden");
x.style.display = "block";
} else if (!data[0]) {
alert("That address was not found in the Whitelist.");
} else if (data[1]) {
alert("You have already voted and may not vote a second time");
} else if (data[2]) {
alert("This election has already concluded. Votes can no longer validly be cast for it.");
}
} catch (error) {
console.log("Promise.all finished with error " + error)
}
}
问题来自获取元掩码帐户的方式:
web3.eth.getAccounts
是一个异步函数。类似的方法应该会奏效:
async function getCurrentAccount() {
console.log("getCurrentAccount method");
let accounts = await web3.eth.getAccounts();
return accounts[0];
}
为什么
getCurrentAccount
是一个async函数
,而它不等待任何东西?另外,它看起来像是web3.eth.getAccounts
异步调用了它的回调,因此您需要像之前使用其他三个函数一样使用new Promise
来提示它。不要忘记声明变量,如currentAccount
或data
withvar
(或let
或const
)BTW,考虑使用破译用于代码>数据< /代码>,这样你就可以给你正在等待的三个值提供有意义的名称。异步并不意味着它返回一个承诺,可以是<代码>等待<代码> ED。在原始代码中,它需要回调?!是的,但是回调是在回调之前触发的,所以是原来的函数。getCurrentAccount
returnundefined
,如果web3.eth.getAccounts
不返回任何内容(因为它需要回调),您的帐户也是如此?