部署智能合约时出错,Cannot read 属性 'deployed' of undefined?
Getting an error while deploying smart contracts, Cannot read property 'deployed' of undefined?
我正在为 Dapp 创建前端,因此在创建智能合约实例并设置提供者之后,我尝试部署合约,获取代币余额并将其显示在网页上,但出现错误:
Cannot read property 'deployed' of undefined
在我的浏览器中。
请注意,我的 FixedSupplyToken.sol 部署在测试 RPC 上,正如我在 FixedSupplyToken.json 文件中看到的那样,它有一个地址。以为这是问题所在,但没有这样的运气......
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 未定义错误。
感谢任何反馈:)
这一行:
import token_artifacts from '../../build/contracts/FixedSupplyToken.json'
我猜您的本地网络服务器的根目录位于 HTML 页面所在的位置。因此,任何将内容拉出 Web 服务器根目录(即从 ..
文件夹)的尝试都将被拒绝。
使用显示网络流量的 F12 工具 运行 重新加载您的页面。我怀疑当页面尝试执行 GET ../../build/contracts/FixedSupplyToken.json
时,您会得到 404 结果
因此,token_artifacts
是未定义的,并且从它创建的所有其他内容也是未定义的。
快速破解方法是将 FixedSupplyToken.json 移动到您的网络根目录并适当调整导入语句。
即使在我将 json 移动到网络根目录之后,我的 python 网络服务器也遇到了另一个问题,python -m http.server
我是 运行 在 Content-Type
header 中返回一个被 javascript 进口商拒绝的 MIME 类型。我做的快速破解只是将 json 声明粘贴到一个 .js 文件中并给它一个变量赋值,如下所示:
window.FizzBuzzContract = // added this line
{
"contractName": "FizzBuzz",
"abi": [...
在 start
函数中,您正在重新分配 TokenContract
变量。
尝试改变
ExchangeContract = web3.setProvider(web3.currentProvider);
TokenContract = web3.setProvider(web3.currentProvider);
至:
ExchangeContract.setProvider(web3.currentProvider);
TokenContract.setProvider(web3.currentProvider);
根据评论编辑:
问题的根源在于 web3.setProvider
方法 returns 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();
我正在为 Dapp 创建前端,因此在创建智能合约实例并设置提供者之后,我尝试部署合约,获取代币余额并将其显示在网页上,但出现错误:
Cannot read property 'deployed' of undefined
在我的浏览器中。
请注意,我的 FixedSupplyToken.sol 部署在测试 RPC 上,正如我在 FixedSupplyToken.json 文件中看到的那样,它有一个地址。以为这是问题所在,但没有这样的运气......
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 未定义错误。
感谢任何反馈:)
这一行:
import token_artifacts from '../../build/contracts/FixedSupplyToken.json'
我猜您的本地网络服务器的根目录位于 HTML 页面所在的位置。因此,任何将内容拉出 Web 服务器根目录(即从 ..
文件夹)的尝试都将被拒绝。
使用显示网络流量的 F12 工具 运行 重新加载您的页面。我怀疑当页面尝试执行 GET ../../build/contracts/FixedSupplyToken.json
因此,token_artifacts
是未定义的,并且从它创建的所有其他内容也是未定义的。
快速破解方法是将 FixedSupplyToken.json 移动到您的网络根目录并适当调整导入语句。
即使在我将 json 移动到网络根目录之后,我的 python 网络服务器也遇到了另一个问题,python -m http.server
我是 运行 在 Content-Type
header 中返回一个被 javascript 进口商拒绝的 MIME 类型。我做的快速破解只是将 json 声明粘贴到一个 .js 文件中并给它一个变量赋值,如下所示:
window.FizzBuzzContract = // added this line
{
"contractName": "FizzBuzz",
"abi": [...
在 start
函数中,您正在重新分配 TokenContract
变量。
尝试改变
ExchangeContract = web3.setProvider(web3.currentProvider);
TokenContract = web3.setProvider(web3.currentProvider);
至:
ExchangeContract.setProvider(web3.currentProvider);
TokenContract.setProvider(web3.currentProvider);
根据评论编辑:
问题的根源在于 web3.setProvider
方法 returns 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();