代码之家  ›  专栏  ›  技术社区  ›  xianshenglu

web3调用得到“错误:返回的值不是可转换字符串:”

  •  0
  • xianshenglu  · 技术社区  · 6 年前

    这是我的合同

    https://kovan.etherscan.io/address/0x9c08fb4e6666a796ef1ade3f58cb0a3e3f469e7c#code

    //address and abi are copied from url above
    let contractAddr = contract.address 
    let contractAbi = contract.abi
    let web3 = new Web3(new Web3.providers.WebsocketProvider('wss://mainnet.infura.io/ws'))
    if (typeof web3 !== 'undefined') {
      web3 = new Web3(web3.currentProvider)
    } else {
      console.log('we need MetaMask')
    }
    
    let myContract = new web3.eth.Contract(contractAbi, contractAddr)
    myContract.methods.name().call().then(console.log).catch(console.log)
    

    我得到这个:

    错误:错误:返回的值不是可转换字符串:

    但是,如果我把合同复制到

    https://remix.ethereum.org/#optimize=true&version=soljson-v0.4.24+commit.e67f0147.js

    使用 ganache . 那么我的代码是:

    //address and abi are copied from url above
    let contractAddr = contract.address
    let contractAbi = contract.abi
    let url = contract.url //http://127.0.0.1:7545 provided by ganache
    let web3
    if (typeof web3 !== 'undefined') {
      // web3 = new Web3(web3.currentProvider)
    } else {
      web3 = new Web3(new Web3.providers.HttpProvider(url))
    }
    let myContract = new web3.eth.Contract(contractAbi, contractAddr)
    myContract.methods.name().call().then(console.log).catch(console.log)
    

    在这种情况下,我会得到正确的结果“momo”。

    我想 infura 作品像 甘纳什 我试过其他的 因弗拉 但都失败了。

    我有 metaMask in my chrome extension and use we web3@^1.0.0-beta.33 .

    如何调用函数

    https://kovan.etherscan.io/address/0x9c08fb4e666a796ef1ade3f58cb0a3e3f469e7c代码

    就像我叫它进来一样

    https://remix.ethereum.org/optimize=true&version=soljson-v0.4.24+commit.e67f0147.js

    通过 甘纳什 .

    1 回复  |  直到 6 年前
        1
  •  1
  •   user94559    6 年前

    let web3 = new Web3(new Web3.providers.WebsocketProvider('wss://mainnet.infura.io/ws'))
    

    let web3 = new Web3(new Web3.providers.WebsocketProvider('wss://kovan.infura.io/ws'))