在控制台中收到此错误:
未捕获的类型错误:TestContract.at 不是函数
我正在使用我从区块链课程中获得的这段代码在测试服务器上实现一个示例合约
var TestContract =new web3.eth.Contract([
{
"constant": false,
"inputs": [
{
"name": "_fName",
"type": "string"
},
{
"name": "_age",
"type": "uint256"
}
],
"name": "setInstructor",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getInstructor",
"outputs": [
{
"name": "",
"type": "string"
},
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
])
var Test = TestContract.at('0xd1d0ba6a5af6bb66490d04b99f4955eb9c9fef36');
您可以添加一个地址作为第二个参数
var TestContract =new web3.eth.Contract([
{
"constant": false,
"inputs": [
{
"name": "_fName",
"type": "string"
},
{
"name": "_age",
"type": "uint256"
}
],
"name": "setInstructor",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getInstructor",
"outputs": [
{
"name": "",
"type": "string"
},
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
],'0xd1d0ba6a5af6bb66490d04b99f4955eb9c9fef36')
您可以在此处阅读有关可用参数的更多信息
或者您可以通过以下方式添加它
TestContract.options.address = '0xd1d0ba6a5af6bb66490d04b99f4955eb9c9fef36'