dApp : Web3.eth.getCoinbase(function(err, account) 没有显示我从 metamask 连接的 rinkeby 账户

dApp : Web3.eth.getCoinbase(function(err, account) not displaying my connected rinkeby account from metamask

我有一个 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

你知道我能做什么吗?

Metamask 不支持 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() returns 挖矿奖励的账户。

web3.eth.getAccounts()[0] returns 您创建的第一个帐户(索引 0)

尝试: var 帐户 = web3.eth.getAccounts()[0];

检查元掩码扩展的设置。 设置->连接->添加'Localhost' 而已。它对我有用 希望这有效。

我花了几个小时来解决这个问题,最后我得到了:

只需在 metamsk

的设置中添加连接 http://localhost:3000

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

好像一切又变了。现在,您可以将 MetaMask 连接到您的销售网站的菜单位置在这里: