NodeJS dApp:Web3.eth.getCoinbase(函数(错误,帐户)没有显示我从元掩码连接的rinkeby帐户

lndjwyie  于 2022-12-22  发布在  Node.js
关注(0)|答案(5)|浏览(122)

我有一个dApp,它可以在本地Ganache网络上运行,但不能在部署在Heroku上的Rinkeby上运行
我在App.js中有以下代码:

// Load account data
    web3.eth.getCoinbase(function(err, account) {
      if (err === null) {
        App.account = account;
        $("#accountAddress").html("Your Account: " + account);
      }
    });

但这就是我得到的您的帐户:零
下面是文件:https://github.com/Louvivien/filmproductiondapp/blob/master/src/js/app.js
你知道我能做什么吗?

3qpi33ja

3qpi33ja1#

默认情况下,Metamask可能不启用连接。请尝试以下操作:

if(web3.currentProvider.enable){
    //For metamask
    web3.currentProvider.enable().then(function(acc){
        App.account = acc[0];
        $("#accountAddress").html("Your Account: " + App.account);
    });
} else{
    App.account = web3.eth.accounts[0];
    $("#accountAddress").html("Your Account: " + App.account);
}

Metamask将弃用web3js支持,转而支持ethereumProvider。使用ethereumProvider的替代方案:

if(window.ethereum){
    ethereum.enable().then(function(acc){
        App.account = acc[0];
        $("#accountAddress").html("Your Account: " + App.account);
    });
}

根据MetaMask文档,返回的帐户对象已被保存为未来扩展的列表,尽管它只服务于一个帐户。
另一种使用MetaMask获取所选地址的快捷方式:

web3.currentProvider.selectedAddress
6qfn3psc

6qfn3psc2#

检查元掩码扩展的设置。设置-〉连接-〉添加'本地主机'就是它。它对我有用希望它有用。

llycmphe

llycmphe3#

我花了几个小时想明白,最后我得到了:
只需在metamsk设置中添加连接http://localhost:3000

mwkjh3gx

mwkjh3gx4#

元掩码不支持web3.eth.getCoinbase(),因为它是一个轻量级客户端https://github.com/MetaMask/faq/blob/master/DEVELOPERS.md#dizzy-all-async---think-of-metamask-as-a-light-client
web3.eth.getCoinbase()返回挖矿奖励的账户。
getAccounts()[0]返回您创建的第一个帐户(索引0)
请尝试:变量帐户= web3.eth.getAccounts()[0];

quhf5bfb

quhf5bfb5#

似乎一切都已经改变了。现在的位置菜单,在那里你可以连接MetaMask到您的销售网站是在这里:

相关问题