I’m currently having problem to connect to my wallet. This is the error:
evmAsk.js:7 Error: Unexpected error
at Um.<anonymous> (evmAsk.js:7:4197)
at Generator.next (<anonymous>)
at s (evmAsk.js:1:1455)
This is my code using React:
import { useState } from "react";
import { ethers } from "ethers";
const Home = () => {
const [walletAddress, setWalletAddress] = useState('');
const [errorMessage, setErrorMessage] = useState('');
const connectWallet = async () => {
if (window.ethereum) {
try {
// Request account access
await window.ethereum.request({ method: 'eth_requestAccounts' });
// Create a new Web3 provider
const provider = new ethers.providers.Web3Provider(window.ethereum);
// Get the signer
const signer = provider.getSigner();
// Get the user's wallet address
const address = await signer.getAddress();
// Set the wallet address in state
setWalletAddress(address);
setErrorMessage(''); // Clear any previous error messages
} catch (error) {
console.error("Error connecting to MetaMask:", error);
setErrorMessage("Failed to connect to MetaMask. Please try again.");
}
} else {
alert('MetaMask is not installed. Please install it to use this app.');
}
};
return (
<div className="app flex flex-col items-center pt-7">
<h1 className="">Connect wallet testing 123 (/≧▽≦)/</h1>
<button className="border border-gray-300 p-2 rounded-lg" onClick={connectWallet}>
Connect MetaMask Wallet
</button>
{walletAddress && (
<p className="mt-4">Connected Wallet Address: {walletAddress}</p>
)}
{errorMessage && (
<p className="mt-4 text-red-500">{errorMessage}</p>
)}
</div>
);
};
export default Home;
I asked copilot and it said it could be the MetaMask conection, ethers library or the way the asynchronous code is handled. I’m honestly new into this and didnt manage to solve this and quite confused.
New contributor
Atan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.