Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript web3.eth.accounts[0]返回未定义,app.vote(1,{from:web3.eth.accounts[0]})给出错误_Javascript_Ethereum_Solidity_Web3js_Metamask - Fatal编程技术网

Javascript web3.eth.accounts[0]返回未定义,app.vote(1,{from:web3.eth.accounts[0]})给出错误

Javascript web3.eth.accounts[0]返回未定义,app.vote(1,{from:web3.eth.accounts[0]})给出错误,javascript,ethereum,solidity,web3js,metamask,Javascript,Ethereum,Solidity,Web3js,Metamask,我是刚接触solidity的,当时正在通过一个简单的网络应用程序进行探索。我正在尝试制作一个候选人投票web应用程序,它接受一些细节,并按下一个按钮,这些细节应该被部署到智能合约创建的块中。 当我试图在truffle控制台中使用web3.eth.accounts[0]获取帐户详细信息时,它返回未定义。 当我试图使用块菌控制台app.vote1,{from:web3.eth.accounts[0]}从帐户1向候选人1发送投票时,我收到如下错误: truffle(development)> w

我是刚接触solidity的,当时正在通过一个简单的网络应用程序进行探索。我正在尝试制作一个候选人投票web应用程序,它接受一些细节,并按下一个按钮,这些细节应该被部署到智能合约创建的块中。 当我试图在truffle控制台中使用web3.eth.accounts[0]获取帐户详细信息时,它返回未定义。 当我试图使用块菌控制台app.vote1,{from:web3.eth.accounts[0]}从帐户1向候选人1发送投票时,我收到如下错误:

truffle(development)> web3.eth.accounts[0]
undefined
truffle(development)> app.vote(1,{ 
from:web3.eth.accounts[0] })
Thrown:
Error: The send transactions "from" field 
must be defined!
at evalmachine.<anonymous>:0:5
at sigintHandlersWrap (vm.js:272:15)
at Script.runInContext (vm.js:127:14)
at runScript
at bound (domain.js:426:14)
    at REPLServer.runBound [as eval] (domain.js:439:12)
    at REPLServer.onLine (repl.js:726:10)
    at REPLServer.emit (events.js:219:5)
    at REPLServer.EventEmitter.emit (domain.js:482:12)
    at REPLServer.Interface._onLine (readline.js:324:10)
    at REPLServer.Interface._line (readline.js:701:8)
    at REPLServer.Interface._ttyWrite (readline.js:1026:14)
    at REPLServer.self._ttyWrite (repl.js:803:7)
    at ReadStream.onkeypress (readline.js:200:10)
    at ReadStream.emit (events.js:219:5)
    at ReadStream.EventEmitter.emit (domain.js:482:12)
    at emitKeys (internal/readline/utils.js:438:14)
    at emitKeys.next (<anonymous>) {
  hijackedStack: 'Error: The send transactions "from" field must be defined!\n' +
    '    at Method.inputTransactionFormatter (C:\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3-core-helpers\\src\\formatters.js:142:1)\n' +
    '    at C:\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3-core-method\\src\\index.js:144:1\n' +
    '    at Array.map (<anonymous>)\n' +
    '    at Method.formatInput (C:\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3-core-method\\src\\index.js:142:1)\n' +
    '    at Method.toPayload (C:\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3-core-method\\src\\index.js:177:1)\n' +
    '    at Eth.send [as sendTransaction] (C:\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3-core-method\\src\\index.js:467:1)\n' +
    '    at Object.sendTransaction (C:\\npm\\node_modules\\truffle\\build\\webpack:\\packages\\contract\\lib\\execute.js:486:1)\n' +
    '    at C:\\npm\\node_modules\\truffle\\build\\webpack:\\packages\\contract\\lib\\execute.js:203:1\n' +
    '    at processTicksAndRejections (internal/process/task_queues.js:97:5)'
}

之所以会出现此错误,是因为未定义from字段,因为在未定义的情况下传递它,正如运行truffledevelopment>web3.eth.accounts[0]时可以看到的那样,它返回未定义的。这是因为这是一种异步方法,这意味着在发送事务时,需要等待accounts方法完成

因此,考虑到web3.eth.accounts已被弃用,您应该这样做&每次键入wait都很乏味:

truffle(development)> let accounts = await web3.eth.getAccounts()
truffle(development)> app.vote(1,{ from: accounts[0] })

之所以会出现此错误,是因为未定义from字段,因为在未定义的情况下传递它,正如运行truffledevelopment>web3.eth.accounts[0]时可以看到的那样,它返回未定义的。这是因为这是一种异步方法,这意味着在发送事务时,需要等待accounts方法完成

因此,考虑到web3.eth.accounts已被弃用,您应该这样做&每次键入wait都很乏味:

truffle(development)> let accounts = await web3.eth.getAccounts()
truffle(development)> app.vote(1,{ from: accounts[0] })

尝试按照以下步骤操作

$truffle编译-重置 $truffle迁移-重置 $let accounts=wait web3.eth.getAccounts $app.vote1,{from:accounts[2]}
每次投票时,都要重新编译和迁移。

尝试按照以下步骤操作

$truffle编译-重置 $truffle迁移-重置 $let accounts=wait web3.eth.getAccounts $app.vote1,{from:accounts[2]}
每次您想要投票时,都要重新编译和迁移。

感谢您的回复,但让accounts=wait web3.eth.getAccounts;抛出:evalmachine.:1等待web3.eth.getAccounts;^SyntaxError:意外标记“;”而truffledevelopment>let accounts=await web3.eth.getAccounts undefined这是正常的,因为控制台等待它没有得到的立即回调。尽管如此,您的命令应该已经执行-尝试键入truffledevelopment>accounts,您应该会看到您的帐户。谢谢Nicolas,它正在工作,您能告诉我如何从特定帐户为候选人投票吗,例如:app.vote1,{from:accounts[0]}不工作。太好了!我认为在评论中讨论这一点会有点混乱,所以我建议你打开一个新问题,展示app.vote应该做什么,调用app.vote时要做什么,以及收到的错误消息。感谢回复,但让accounts=wait web3.eth.getAccounts;抛出:evalmachine.:1等待web3.eth.getAccounts;^SyntaxError:意外标记“;”而truffledevelopment>let accounts=await web3.eth.getAccounts undefined这是正常的,因为控制台等待它没有得到的立即回调。尽管如此,您的命令应该已经执行-尝试键入truffledevelopment>accounts,您应该会看到您的帐户。谢谢Nicolas,它正在工作,您能告诉我如何从特定帐户为候选人投票吗,例如:app.vote1,{from:accounts[0]}不工作。太好了!我认为在评论中讨论这个问题会有点混乱,所以我建议你打开一个新问题,展示app.vote应该做什么,调用它要做什么,以及收到的错误消息。