简体   繁体   English

部署智能合约时出错,无法读取未定义的“已部署”属性?

[英]Getting an error while deploying smart contracts, Cannot read property 'deployed' of undefined?

Im making a frontend for a Dapp, so after making instances of the smart contracts and setting a provider I try to deploy the contract, get the token balance and display it in on the web page but get the error: Cannot read property 'deployed' of undefined in my browser.我正在为 Dapp 制作前端,因此在制作智能合约的实例并设置提供者后,我尝试部署合约,获取令牌余额并将其显示在网页上,但出现错误: Cannot read property 'deployed' of undefined在我的浏览器Cannot read property 'deployed' of undefined

Note that my FixedSupplyToken.sol is deployed on test RPC as I can see in the FixedSupplyToken.json file, it has an address.请注意,正如我在 FixedSupplyToken.json 文件中看到的那样,我的 FixedSupplyToken.sol 部署在测试 RPC 上,它有一个地址。 Was thinking this was the issue, but no such luck...以为这是问题所在,但没有这样的运气......

app.js:

    // Import libraries we need.
    import { default as Web3} from 'web3';
    import { default as contract } from 'truffle-contract'
    
    // Import our contract artifacts and turn them into usable abstractions.
    import exchange_artifacts from '../../build/contracts/Exchange.json'
    import token_artifacts from '../../build/contracts/FixedSupplyToken.json'
    
    
    var accounts;
    var account;
    
    var ExchangeContract = contract(exchange_artifacts);
    var TokenContract = contract(token_artifacts);
    window.App = {
      start: function() {
       //bootstrap everything
       
       ExchangeContract = web3.setProvider(web3.currentProvider);
       TokenContract = web3.setProvider(web3.currentProvider);

},

 //update balance function

 updateTokenBalance: function() {
    var tokenInstance;
    **TokenContract.deployed().then(function (instance) { // getting the Uncaught TypeError: Cannot read property 'deployed' of undefined**
        tokenInstance = instance;
        return tokenInstance.balanceOf.call(account);
    }).then(function (value) {
        
        var balance_element = document.getElementById("balanceTokenInToken");
        balance_element.innerHTML = value.valueOf();
    }).catch(function (e) {
        console.log(e);
        App.setStatus("Error getting balance; see log.");
    });
  },

在此处输入图片说明

So I have tried :所以我试过: 在此处输入图片说明

在此处输入图片说明

Even if I uncomment web3.setProvider(web3.currentProvider) I get the setProvider undefined error.即使我取消注释 web3.setProvider(web3.currentProvider) 我也会收到 setProvider undefined 错误。

Thanks for any feedback :)感谢您的任何反馈:)

In the start function, you're reassigning the TokenContract variable.start函数中,您正在重新分配TokenContract变量。

Try changing尝试改变

ExchangeContract = web3.setProvider(web3.currentProvider);
TokenContract = web3.setProvider(web3.currentProvider);

to:到:

ExchangeContract.setProvider(web3.currentProvider);
TokenContract.setProvider(web3.currentProvider);

Edit based on comment:根据评论编辑:

The root of the problem is that web3.setProvider method returns void or undefined , so in the statement TokenContract = web3.setProvider(web3.currentProvider);问题的根源在于web3.setProvider方法返回 void 或undefined ,所以在语句TokenContract = web3.setProvider(web3.currentProvider); you are assigning undefined to TokenContract , hence the error.您将undefined分配给TokenContract ,因此出现错误。 Setting the web3 provider and the Truffle contract provider are two different actions.设置 web3 提供者和 Truffle 合约提供者是两个不同的操作。 Not sure if there's more code in the //bootstrap everything but if for some reason you need to set the provider explicitly try changing the code to:不确定//bootstrap everything是否有更多代码,但如果出于某种原因您需要显式设置提供程序,请尝试将代码更改为:

web3.setProvider(web3.currentProvider);
ExchangeContract.setProvider(web3.currentProvider);
TokenContract.setProvider(web3.currentProvider);

The first line, though, shouldn't be necessary if Web3 is configured correctly.但是,如果 Web3 配置正确,则不需要第一行。 I suggest reading this article for this since there's been changes in how this is done: https://medium.com/@awantoch/how-to-connect-web3-js-to-metamask-in-2020-fee2b2edf58a .我建议为此阅读这篇文章,因为这样做的方式发生了变化: https : //medium.com/@awantoch/how-to-connect-web3-js-to-metamask-in-2020-fee2b2edf58a

The gist of it is:它的要点是:

window.web3 = new Web3(window.ethereum);
window.ethereum.enable();

This line:这一行:

import token_artifacts from '../../build/contracts/FixedSupplyToken.json'

I'm guessing your local webserver is rooted where your HTML page is.我猜您的本地网络服务器植根于您的 HTML 页面所在的位置。 Hence, any attempt to pull content outside of the web server root (ie from the .. folder) is going to get declined.因此,任何将内容拉出 Web 服务器根目录(即从..文件夹)的尝试都将被拒绝。

Reload your page with the the F12 tools running showing network traffic.使用 F12 工具运行显示网络流量重新加载您的页面。 I suspect you're going to get a 404 result when the page attempts to do a GET ../../build/contracts/FixedSupplyToken.json我怀疑当页面尝试执行GET ../../build/contracts/FixedSupplyToken.json时,您会得到 404 结果

Hence, token_artifacts undefined, and everything else created off of it is also undefined.因此, token_artifacts未定义,其他所有由它创建的东西也未定义。

The quick hack is to move FixedSupplyToken.json into your web root and adjust the import statement appropriately.快速的技巧是将 FixedSupplyToken.json 移动到您的 Web 根目录并适当调整导入语句。

The other issue I had with my python web server with that even after I had moved the json to the web root, the python -m http.server thing I was running was returning a mime type in the Content-Type header rejected by the javascript importer.我的 python Web 服务器遇到的另一个问题是,即使在我将 json 移动到 Web 根目录之后,我正在运行的python -m http.server东西在Content-Type标头中返回了一个 mime 类型,但被 javascript 拒绝进口商。 The quick hack I did was just to paste the json declaration into a .js file and give it a variable assignment such as the following:我所做的快速黑客只是将 json 声明粘贴到 .js 文件中,并给它一个变量赋值,如下所示:

window.FizzBuzzContract =    // added this line
{
  "contractName": "FizzBuzz",
  "abi": [...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在运行使用solidity 智能合约的next.js 应用程序时,出现“无法读取未定义的属性”错误 - While running my next.js app that is using solidity smart contracts, I am getting "Cannot read properties of undefined" error TypeError:无法读取未定义的属性“长度”-使用安全帽进行部署时 - TypeError: Cannot read property 'length' of undefined - While deploying using hardhat 获取错误无法读取未定义的属性“ getTime” - Getting error Cannot read property 'getTime' of undefined 出现错误“无法读取未定义的属性&#39;appendchild&#39; - Getting error "Cannot read property 'appendchild' of undefined 收到错误“无法读取未定义的属性&#39;模板&#39;” - Getting error “Cannot read property 'template' of undefined” 获取错误“无法读取未定义的属性附加子项 - getting error "Cannot read property appendchild of undefined 收到“无法读取未定义的属性 'getFoodX'”错误 - getting a “Cannot read property 'getFoodX' of undefined” error 获取 Cannot read property getDate of undefined 错误 - getting Cannot read property getDate of undefined error 获取错误无法读取未定义的属性“等于” - Getting Error Cannot read property 'equals' of undefined 无法读取属性“#” <Object> 的不确定错误? - Cannot read property '#<Object>' of undefined getting error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM