![](/img/trans.png)
[英]While running my next.js app that is using solidity smart contracts, I am getting "Cannot read properties of undefined" error
[英]Getting an error while deploying smart contracts, Cannot read property 'deployed' of undefined?
我正在为 Dapp 制作前端,因此在制作智能合约的实例并设置提供者后,我尝试部署合约,获取令牌余额并将其显示在网页上,但出现错误: Cannot read property 'deployed' of undefined
在我的浏览器Cannot read property 'deployed' of undefined
。
请注意,正如我在 FixedSupplyToken.json 文件中看到的那样,我的 FixedSupplyToken.sol 部署在测试 RPC 上,它有一个地址。 以为这是问题所在,但没有这样的运气......
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.");
});
},
即使我取消注释 web3.setProvider(web3.currentProvider) 我也会收到 setProvider undefined 错误。
感谢您的任何反馈:)
在start
函数中,您正在重新分配TokenContract
变量。
尝试改变
ExchangeContract = web3.setProvider(web3.currentProvider);
TokenContract = web3.setProvider(web3.currentProvider);
到:
ExchangeContract.setProvider(web3.currentProvider);
TokenContract.setProvider(web3.currentProvider);
根据评论编辑:
问题的根源在于web3.setProvider
方法返回 void 或undefined
,所以在语句TokenContract = web3.setProvider(web3.currentProvider);
您将undefined
分配给TokenContract
,因此出现错误。 设置 web3 提供者和 Truffle 合约提供者是两个不同的操作。 不确定//bootstrap everything
是否有更多代码,但如果出于某种原因您需要显式设置提供程序,请尝试将代码更改为:
web3.setProvider(web3.currentProvider);
ExchangeContract.setProvider(web3.currentProvider);
TokenContract.setProvider(web3.currentProvider);
但是,如果 Web3 配置正确,则不需要第一行。 我建议为此阅读这篇文章,因为这样做的方式发生了变化: https : //medium.com/@awantoch/how-to-connect-web3-js-to-metamask-in-2020-fee2b2edf58a 。
它的要点是:
window.web3 = new Web3(window.ethereum);
window.ethereum.enable();
这一行:
import token_artifacts from '../../build/contracts/FixedSupplyToken.json'
我猜您的本地网络服务器植根于您的 HTML 页面所在的位置。 因此,任何将内容拉出 Web 服务器根目录(即从..
文件夹)的尝试都将被拒绝。
使用 F12 工具运行显示网络流量重新加载您的页面。 我怀疑当页面尝试执行GET ../../build/contracts/FixedSupplyToken.json
时,您会得到 404 结果
因此, token_artifacts
未定义,其他所有由它创建的东西也未定义。
快速的技巧是将 FixedSupplyToken.json 移动到您的 Web 根目录并适当调整导入语句。
我的 python Web 服务器遇到的另一个问题是,即使在我将 json 移动到 Web 根目录之后,我正在运行的python -m http.server
东西在Content-Type
标头中返回了一个 mime 类型,但被 javascript 拒绝进口商。 我所做的快速黑客只是将 json 声明粘贴到 .js 文件中,并给它一个变量赋值,如下所示:
window.FizzBuzzContract = // added this line
{
"contractName": "FizzBuzz",
"abi": [...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.