简体   繁体   English

web3.js中包含合同的Contract.at的替代功能是什么?

[英]What is the replacement function for Contract.at in web3.js for including a contract?

Getting this error in the console : Uncaught TypeError: TestContract.at is not a function 在控制台中收到此错误:未捕获的TypeError:TestContract.at不是函数

I am implementing a sample contract on a test server using this code i got from a course which I'm doing on Blockchain 我正在使用我在Blockchain上完成的课程获得的代码在测试服务器上实现示例合同

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');

You can just add an address as the second parameter 您只需添加一个地址作为第二个参数

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')

You can read more about the available parameters there 您可以在此处详细了解可用参数

Or you can add it via 或者您可以通过添加

TestContract.options.address = '0xd1d0ba6a5af6bb66490d04b99f4955eb9c9fef36'

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM