我正在尝试创建一个 Dapp,并且我正在添加一个按钮来连接到 MetaMask。但是我去的时候遇到了一个错误localhost:3000
,我得到了ReferenceError: document is not defined
。
我使用这个模板app.js在不同的 javascript 文件之间导航,这就是你打开 Dapp 时会看到的内容。
// The first thing we might think about is setting up a layout
//so that we can enable some navigation that will persist across all pages.
import '../styles/globals.css'
import Link from 'next/link'
function MyApp({ Component, pageProps }) {
return (
<div>
<nav className="border-b p-6">
<p className="text-4xl font-bold">KNOME</p>
<div className="flex mt-4">
<Link href="/">
<a className="mr-4 text-pink-500">
Home
</a>
</Link>
<Link href="/create-item">
<a className="mr-6 text-pink-500">
Sell Digital Asset
</a>
</Link>
<Link href="/my-assets">
<a className="mr-6 text-pink-500">
My Digital Assets
</a>
</Link>
<Link href="/connect-metamask">
<a className="mr-6 text-pink-500">
Connect Metamask
</a>
</Link>
</div>
</nav>
<Component {...pageProps} />
</div>
)
}
export default MyApp
我从https://docs.metamask.io/guide/create-dapp.html#project-setup复制了关于将 Metamask 连接到 Dapp 的教程,我有一个名为connect-metamask.js的 JS 文件
import MetaMaskOnboarding from '@metamask/onboarding'
const forwarderOrigin = 'http://localhost:3000'
const onboardButton = document.getElementById('connectButton');
const getAccountsButton = document.getElementById('getAccounts');
const getAccountsResult = document.getElementById('getAccountsResult');
const initialize = () => {
//Created check function to see if the MetaMask extension is installed
const isMetaMaskInstalled = () => {
//Have to check the ethereum binding on the window object to see if it's installed
const { ethereum } = window;
return Boolean(ethereum && ethereum.isMetaMask);
};
//------Inserted Code------\\
const MetaMaskClientCheck = () => {
//Now we check to see if Metmask is installed
if (!isMetaMaskInstalled()) {
//If it isn't installed we ask the user to click to install it
onboardButton.innerText = 'Click here to install MetaMask!';
//When the button is clicked we call th is function
onboardButton.onclick = onClickInstall;
//The button is now disabled
onboardButton.disabled = false;
} else {
//If MetaMask is installed we ask the user to connect to their wallet
onboardButton.innerText = 'Connect';
//When the button is clicked we call this function to connect the users MetaMask Wallet
onboardButton.onclick = onClickConnect;
//The button is now disabled
onboardButton.disabled = false;
}
};
MetaMaskClientCheck();
//------/Inserted Code------\\
//Eth_Accounts-getAccountsButton
getAccountsButton.addEventListener('click', async () => {
//we use eth_accounts because it returns a list of addresses owned by us.
const accounts = await ethereum.request({ method: 'eth_accounts' });
//We take the first address in the array of addresses and display it
getAccountsResult.innerHTML = accounts[0] || 'Not able to get accounts';
});
}
//We create a new MetaMask onboarding object to use in our app
const onboarding = new MetaMaskOnboarding({ forwarderOrigin });
//This will start the onboarding proccess
const onClickInstall = () => {
onboardButton.innerText = 'Onboarding in progress';
onboardButton.disabled = true;
//On this object we have startOnboarding which will start the onboarding process for our end user
onboarding.startOnboarding();
};
const onClickConnect = async () => {
try {
// Will open the MetaMask UI
// You should disable this button while the request is pending!
await ethereum.request({ method: 'eth_requestAccounts' });
} catch (error) {
console.error(error);
}
};
window.addEventListener('DOMContentLoaded', initialize)