Untuck TypeError:Web3不是构造函数。区块链网站无法连接到MetAmask
我在 web3.eth.defaultAccount = web3.eth.getAccounts(); 上遇到问题,
下面是代码。它说未捕获(承诺)TypeError:无法读取未定义的属性(读取'getAccounts')
我的web3的版本是“^1.3”。
var contract = "";
if (typeof web3 !== 'undefined') {
console.log('inside web3')
Web3 = new Web3 (Web3.currentProvider);
} else {
console.log('else web3');
var web3 = new Web3(new Web3.providers.HttpProvider(provider));
}
window.ethereum.enable()
.then(function (accounts) {
console.log(accounts[0]);
web3.eth.defaultAccount = web3.eth.getAccounts();
var contractabi = web3.eth.contract([ABI])
i have issue on web3.eth.defaultAccount = web3.eth.getAccounts();
below is the code. it said Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'getAccounts')
the version of my web3 is "^1.3".
var contract = "";
if (typeof web3 !== 'undefined') {
console.log('inside web3')
Web3 = new Web3 (Web3.currentProvider);
} else {
console.log('else web3');
var web3 = new Web3(new Web3.providers.HttpProvider(provider));
}
window.ethereum.enable()
.then(function (accounts) {
console.log(accounts[0]);
web3.eth.defaultAccount = web3.eth.getAccounts();
var contractabi = web3.eth.contract([ABI])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
函数必须是异步的
尝试这个来适合你的情况
function must be async
try this to fit your case
如果可能的话,请向我发送此代码的完整实现,或者您可以添加此代码
,请使用异步函数与 web3 交互并等待响应
if possible send me the complete implementation of this code, or you can add this
please use async function to interact with web3 and await the response