部署()返回未定义的松露
我正在尝试使用松露和Ganache开发DAPP。 Deployed()正在返回未定义的,因此我不能将其用于创建合同实例。
这是代码:
App = {
web3Provider: null,
contracts: {},
init: async function() {
$('#category').append('<option value="Clothing">Clothing</option>');
return await App.initWeb3();
},
initWeb3: async function() {
if (window.ethereum) {
App.web3Provider = window.ethereum;
try {
await window.ethereum.request({ method: "eth_requestAccounts" });
} catch (error) {
console.error("User denied account access");
}
}
else if (window.web3) {
App.web3Provider = window.web3.currentProvider;
}
else {
App.web3Provider = new Web3.providers.HttpProvider('http://localhost:8545');
}
web3 = new Web3(App.web3Provider);
return await App.initContract();
},
initContract: function() {
$.getJSON('Marketplace.json', function(data) {
// Get the necessary contract artifact file and instantiate it with @truffle/contract
var MarketplaceArtifact = data;
App.contracts.Marketplace = TruffleContract(MarketplaceArtifact);
// Set the provider for our contract
App.contracts.Marketplace.setProvider(App.web3Provider);
});
return App.initUI();
},
initUI: function() {
var marketplaceInstance;
App.contracts.Marketplace.deployed().then(function(instance) {
marketplaceInstance = instance;
return marketplaceInstance.getCategories.call();
}).then(function(categories) {
alert(categories[1]);
for(i = 0; i < categories.length; i++) {
$('#category').append('<option value="' + categories[i] + '">' + categories[i] + '</option>');
}
}).catch(function(err) {
console.log(err.message);
});
}
};
$(function() {
$(window).load(function() {
App.init();
});
});
我得到以下例外,如下所示:
)。
app.contracts.marketplace.deployed
( “部署”) 在Object.Initui(regission-nonprofit.js:49:31)
decloyed()函数应返回合同实例,但不是。请帮忙。
I am trying to develop a dApp using truffle and ganache. deployed() is returning undefined so I cannot use that for creating an instance of the contract.
Here is the code:
App = {
web3Provider: null,
contracts: {},
init: async function() {
$('#category').append('<option value="Clothing">Clothing</option>');
return await App.initWeb3();
},
initWeb3: async function() {
if (window.ethereum) {
App.web3Provider = window.ethereum;
try {
await window.ethereum.request({ method: "eth_requestAccounts" });
} catch (error) {
console.error("User denied account access");
}
}
else if (window.web3) {
App.web3Provider = window.web3.currentProvider;
}
else {
App.web3Provider = new Web3.providers.HttpProvider('http://localhost:8545');
}
web3 = new Web3(App.web3Provider);
return await App.initContract();
},
initContract: function() {
$.getJSON('Marketplace.json', function(data) {
// Get the necessary contract artifact file and instantiate it with @truffle/contract
var MarketplaceArtifact = data;
App.contracts.Marketplace = TruffleContract(MarketplaceArtifact);
// Set the provider for our contract
App.contracts.Marketplace.setProvider(App.web3Provider);
});
return App.initUI();
},
initUI: function() {
var marketplaceInstance;
App.contracts.Marketplace.deployed().then(function(instance) {
marketplaceInstance = instance;
return marketplaceInstance.getCategories.call();
}).then(function(categories) {
alert(categories[1]);
for(i = 0; i < categories.length; i++) {
$('#category').append('<option value="' + categories[i] + '">' + categories[i] + '</option>');
}
}).catch(function(err) {
console.log(err.message);
});
}
};
$(function() {
$(window).load(function() {
App.init();
});
});
I am getting an exception as follows in this line:
App.contracts.Marketplace.deployed().then(function(instance)
The exception is:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'deployed')
at Object.initUI (register-nonprofit.js:49:31)
the deployed() function should return the contract instance, but it is not. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论