部署()返回未定义的松露

发布于 2025-01-26 05:04:37 字数 2097 浏览 3 评论 0原文

我正在尝试使用松露和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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文