1

This is the error I am getting in my console.. I am unable to understand the bug

inpage.js:8 Uncaught (in promise) Error: 'args.method' must be a non-empty string.
    at o (inpage.js:8:31826)
    at Object.invalidRequest (inpage.js:8:32276)
    at l.request (inpage.js:1:37391)
    at HTMLButtonElement.Connect (index.js:13:20)

My index.js

const button = document.getElementById("ConnectButton");
//button.addEventListener('click',connect);
if (button) {
  console.log("button successfully imported");
} else {
  console.log("BUtton is not imported");
}

const Connect = async () => {
  if (typeof window.ethereum !== "undefined") {
    await ethereum.request({ methord: "eth_requestAccounts" });

    button.innerHTML = "Connected";
  } else {
    button.innerHTML = "Pls install metamask";
  }
};
button.addEventListener("click", Connect);

My index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FUND ME</title>
</head>
<body>
    <h1>HELLO WORLD
    </h1>
    <button id="ConnectButton">Connect</button>
    <script src="index.js"></script>
</body>
</html> 

1 Answer 1

1

You did not handle the promise properly.

Instead, use this code.

const button = document.getElementById("ConnectButton");
if (button) {
  console.log("button successfully imported");
} else {
  console.log("BUtton is not imported");
}

const Connect = async () => {
  if (typeof window.ethereum !== "undefined") {
    button.addEventListener("click", async () => {
      //Will Start the metamask extension
      await ethereum.request({ method: "eth_requestAccounts" });
      button.textContent = "Connected";
    });
  } else {
    button.textContent = "Pls install metamask";
  }
};

Connect().catch((err) => {
  console.log(err);
  process.exit(1);
});

Bonus:

If you observe carefully, I replaced innerHtml with textContent. The reason behind is that not only textContent performs well relatively, since it doesn't parse the value into the html. For the same reason, it can prevent XSS attacks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.