current position:Home>Contract has not been deployed to detected network (network / artifact mismatch)

Contract has not been deployed to detected network (network / artifact mismatch)

2022-02-03 23:56:20 Q & A of Denglian community

markAdopted: function(adopters, account) {    var adoptionInstance;    App.contracts.Adoption.deployed().then(function(instance) {      adoptionInstance = instance;      //  Call the contract getAdopters(),  use call Read information without consumption gas      return adoptionInstance.getAdopters.call();    }).then(function(adopters) {      for (i = 0; i < adopters.length; i++) {        if (adopters[i] !== '0x0000000000000000000000000000000000000000') {          $('.panel-pet').eq(i).find('button').text('Success').attr('disabled', true);        }      }    }).catch(function(err) {      console.log('contract fail',err.message);    });  },

Intercepted from above app.js file

Always reporting mistakes contract fail Contract has not been deployed to detected network (network/artifact mismatch), But I can't solve the above code , I use the contract truffle The normal compilation is deployed locally , But still , The complete code is as follows (https://www.jianshu.com/p/9844d97ef1d0


When you try, put App.contracts.Adoption.deployed().then(function(instance) Changed to App.contracts.Adoption.at(" My contract deployment address ").then(function(instance) This is the time instance An instance can be returned normally , But with deployed(),instance You can't return an instance normally , Here is the error report , I hope some big guys can answer their doubts .


App = {  web3Provider: null,  contracts: {},  init: async function() {    // Load pets.    $.getJSON('../pets.json', function(data) {      var petsRow = $('#petsRow');      var petTemplate = $('#petTemplate');      for (i = 0; i < data.length; i ++) {        petTemplate.find('.panel-title').text(data[i].name);        petTemplate.find('img').attr('src', data[i].picture);        petTemplate.find('.pet-breed').text(data[i].breed);        petTemplate.find('.pet-age').text(data[i].age);        petTemplate.find('.pet-location').text(data[i].location);        petTemplate.find('.btn-adopt').attr('data-id', data[i].id);        petsRow.append(petTemplate.html());      }    });    return await App.initWeb3();  },  initWeb3: function() {    // Is there an injected web3 instance?    if (typeof web3 !== 'undefined') {      App.web3Provider = web3.currentProvider;    } else {      // If no injected web3 instance is detected, fall back to Ganache      //  Priority call MetaMask Provided web3 Example       App.web3Provider = new Web3.providers.HttpProvider('http://localhost:8545');    }    web3 = new Web3(App.web3Provider);    return App.initContract();  },  initContract: function() {    //  load Adoption.json, Save the Adoption Of ABI( Interface specification ) Information and deployed network ( Address ) Information , It generates... When compiling the contract ABI, Add network information during deployment   $.getJSON('../Adoption.json', function(data) {    //  use Adoption.json Data creates an interactive TruffleContract Contract example .    var AdoptionArtifact = data;    console.log();    App.contracts.Adoption = TruffleContract(AdoptionArtifact);    console.log(App.contracts.Adoption);    // Set the provider for our contract    App.contracts.Adoption.setProvider(App.web3Provider);    // Use our contract to retrieve and mark the adopted pets    return App.markAdopted();  });  return App.bindEvents();  },  bindEvents: function() {    $(document).on('click', '.btn-adopt', App.handleAdopt);  },  markAdopted: function(adopters, account) {    var adoptionInstance;    App.contracts.Adoption.deployed().then(function(instance) {      adoptionInstance = instance;      //  Call the contract getAdopters(),  use call Read information without consumption gas      return adoptionInstance.getAdopters.call();    }).then(function(adopters) {      for (i = 0; i < adopters.length; i++) {        if (adopters[i] !== '0x0000000000000000000000000000000000000000') {          $('.panel-pet').eq(i).find('button').text('Success').attr('disabled', true);        }      }    }).catch(function(err) {      console.log('contract fail',err.message);    });  },  handleAdopt: function(event) {    event.preventDefault();    var petId = parseInt($(event.target).data('id'));    var adoptionInstance;    //  Get user account     web3.eth.getAccounts(function(error, accounts) {      if (error) {        console.log('no get account',error);      }      var account = accounts[0];      App.contracts.Adoption.deployed().then(function(instance) {        adoptionInstance = instance;        //  Send a deal to adopt a pet         return adoptionInstance.adopt(petId, {from: account});      }).then(function(result) {        return App.markAdopted();      }).catch(function(err) {        console.log('adoption fail',err.message);      });    });  },};$(function() {  $(window).load(function() {    App.init();  });});





Other answers 1:

copyright notice
author[Q & A of Denglian community],Please bring the original link to reprint, thank you.
https://en.netfreeman.com/2022/02/202202032356185874.html

Random recommended