简体   繁体   中英

Unable to connect Ganache with Truffle/Npm Dev server

I am able to work with Truffle and Ganache-cli. Have deployed the contract and can play with that using truffle console

truffle(development)> 
Voting.deployed().then(function(contractInstance) 
{contractInstance.voteForCandidate('Rama').then(function(v) 
{console.log(v)})})
undefined
truffle(development)> { tx: 
'0xe4f8d00f7732c09df9e832bba0be9f37c3e2f594d3fbb8aba93fcb7faa0f441d',
  receipt: 
   { transactionHash: 
'0xe4f8d00f7732c09df9e832bba0be9f37c3e2f594d3fbb8aba93fcb7faa0f441d',
     transactionIndex: 0,
     blockHash: 
'0x639482c03dba071973c162668903ab98fb6ba4dbd8878e15ec7539b83f0e888f',
     blockNumber: 10,
     gasUsed: 28387,
     cumulativeGasUsed: 28387,
     contractAddress: null,
     logs: [],
     status: '0x01',
     logsBloom: ... }

Now when i started a server using "npm run dev". Server started fine but is not connecting with the Blockchain

i am getting the error

Uncaught (in promise) Error: Contract has not been deployed to detected network (network/artifact mismatch)

This is my truffle.js

// Allows us to use ES6 in our migrations and tests.
require('babel-register')

module.exports = {
 networks: {
    development: {
      host: '127.0.0.1',
      port: 8545,
      network_id: '*', // Match any network id
      gas: 1470000

    }
  }
}

Can you please guide me how i can connect ?

In your truffle.js , change 8545 to 7545 .

Or , in Ganache (GUI), click the gear in the upper right corner and change the port number from 7545 to 8545 , then restart. With ganache-cli use -p 8545 option on startup to set 8545 as the port to listen on.

Either way, the mismatch seems to be the issue; these numbers should match. This is a common issue.

Also feel free to check out ethereum.stackexchange.com . If you want your question moved there, you can flag it and leave a message for a moderator to do that.

Solve the issue.

issue was at currentProvider, i gave the url of ganache blockchain provider and it worked.

if (typeof web3 !== 'undefined') {
console.warn("Using web3 detected from external source like Metamask")
// Use Mist/MetaMask's provider
// window.web3 = new Web3(web3.currentProvider);
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));

  } else {
console.warn("No web3 detected. Falling back to http://localhost:8545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM