Metask problem: window.ethereum.request ({method:" eth_requestaccounts "})
does not open
As a web developer using react, you probably have frustration when you try to interact with the Metask wallet. The problem lies in how Metask integrates with the web platform of the browser. Here is an article that explains the problem and proposes a solution.
The problem: window.ethereum.request ({method:" eth_requestaccounts "})
does not open
When clicking the button to trigger Window.ethereum.request ({method:" Eth_requestaccounts "})
, try to execute the request. However, this request does not seem to open the metamark correctly.
Why request does not open
The reason for this problem is that “Window.ethereum.request ()” expects a specific URL as an argument, which is used by Metask to establish a connection with your browser. with the real demand made by Metask when clicking the button.
Solution: Using windows.ethereum.send ({method: 'eth_requestaccounts'})
To solve this problem, you must use the correct function that corresponds to your request. In this case, try to get an account from Metask.
Here’s how you can change your code:
`Jsx
Import reacts, {useState} from "react";
Import the window from the "window";
Import {Eters} from '@metamans/daps';
Function App () {
Const [account, setaccount] = usestate ('');
// Get the user's Ethereum Account Address
const gataccount = async () => {
Try {
Constururi = await the window.ethereum.request ({method: "eth_requestaccounts"});
if (cont.length> 0) {
setaccount (accounts [0] .address);
}
} Catch (error) {
console.error (error);
}
};
// button to trigger your account application
Const HandorbuttonClick = () => {
Getaccount ();
}
Return (
{account? (
account: {account}
): null}
);
}
Default export application;
Changes made
We made the following changes to our code:
- I replacedWindow.ethereum.request ({method:” eth_requestaccounts “})
with
window.ethereum.send ({method: ‘eth_requestaccounts’}).
- In the functionGetaccount ()`, we use the correct URL for the Metask request.
- After obtaining the user’s account address, we display it on the page.
By making these changes, you should be able to get an Ethereum account from Metask when clicking the button now.