-1

Getting this error in the console:

Uncaught TypeError: TestContract.at is not a function

I am implementing a sample contract on a test server using this code i got from a course which I'm doing on 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');
TylerH
  • 20,799
  • 66
  • 75
  • 101
Aryan566
  • 1
  • 1
  • Does this answer your question? [JavaScript error: "is not a function"](https://stackoverflow.com/questions/9825071/javascript-error-is-not-a-function) – TylerH Jan 09 '23 at 14:45

1 Answers1

2

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'
Vitaly Migunov
  • 4,297
  • 2
  • 19
  • 23