简体   繁体   English

当我在浏览器中切换 Metamask 地址时,Web3 getAccounts 不会更新

[英]Web3 getAccounts doesn't update when I switch Metamask addresses in browser

I have a node app that connects to a basic frontend, and one of the get calls in the server is to get the address of the user.我有一个连接到基本前端的节点应用程序,服务器中的get调用之一是获取用户的地址。 It works the first time, however, when I manually switch addresses/accounts in Metamask plugin for the website, it doesn't update, and stays on the old address.它第一次工作,但是,当我在网站的 Metamask 插件中手动切换地址/帐户时,它不会更新,而是停留在旧地址上。

For reference I am simply using and connecting to a ganache test network opened on my laptop.作为参考,我只是使用并连接到在我的笔记本电脑上打开的 ganache 测试网络。

Relevant code below, first the server.js :下面的相关代码,首先是server.js

const express = require('express')
const app = express()
const port = 3000
const Web3 = require('web3');

const WEB3_PROVIDER = "HTTP://127.0.0.1:7545"
if (typeof web3 !== 'undefined') {
    web3 = new Web3(web3.currentProvider);
    console.log("web3 already initialized.");
} else {
    // set the provider you want from Web3.providers
    web3 = new Web3(new Web3.providers.HttpProvider(WEB3_PROVIDER));
    console.log("New web3 object initialized.");
}

app.get('/', (req, res) => {
  res.sendFile('./main.html', { root: __dirname });
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

app.get('/get-account', async (_req, res) => {
    try {
        web3.eth.getAccounts().then(function(accs){ 
            this_acc = accs[0];
            console.log("account :: "); console.log(this_acc); 
            return res.send(this_acc);
        })
    
    } catch (e) { throw e; }
});

Then the main.html page calls client.js which has the following relevant snippet:然后main.html页面调用具有以下相关片段的client.js

async function updateUserAddr() {
    console.log("updateUserAddr");
    const response = await fetch('/get-account');
    var addr_str = await response.text();
    console.log(addr_str);
    $('#address_id_poster').text(addr_str);
}

updateUserAddr();

The first time run it logs the correct address, letting me display it on the html page.第一次运行它记录正确的地址,让我在html页面上显示它。 But then I then remove that account from Metamask, add a different one, and restart and refresh and it shows the same old account.但随后我从 Metamask 中删除了该帐户,添加了一个不同的帐户,然后重新启动并刷新,它显示了相同的旧帐户。

Any reason why this code wouldn't update the account change in Metamask?此代码不会更新 Metamask 中的帐户更改的任何原因? How can I fix this?我怎样才能解决这个问题? Help much appreciated.非常感谢帮助。

TI think when the browser downloads anything, it caches, and fetch() is also caching the result. TI 认为当浏览器下载任何东西时,它都会缓存,而 fetch() 也会缓存结果。 Change the options of fetch:更改 fetch 的选项:

const response = await fetch(url, {
    method: 'GET',
    // this should invalidate caching
    cache: 'no-cache',     
    
  });

This should make a new request everytime you refresh the page每次刷新页面时,这都应该发出一个新请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM