web3.js – Uncaught Type Error When Calling Contract Function Using JavaScript API

contract-developmentcontract-invocationjavascriptnodejsweb3js

I wrote a contract to enable registration of a name. As an example, one of the functions takes in a name as a string, then outputs information on the registration of the name (name, registrant, last time that name was updated, registration ID, and whether or not it's currently registered). I can call the function in the Truffle console just fine, but keep getting an Uncaught TypeError when using the web3 JavaScript API to call the same function and I don't know why.

Here's what it looks like in the Truffle console:

Image 1 (Truffle)

When I try to call this same function in a JavaScript console in Chrome (using React to develop a front-end), I get this error and I'm not sure why:

inpage.js:7928 Uncaught TypeError: Cannot read property 'match' of undefined

Here's an image showing the function I'm calling with the error:

Image 2 (JavaScript console)

The nameRegContract object was created like this for reference:

var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
var nameRegABI = [... ABI ...];
var nameRegAddress = '0x23fa6b40bdf47a6a41feb62077dab84213daa624';
var nameRegContract = web3.eth.contract(nameRegABI).at(nameRegAddress);

Wondering if anyone knows why I'm getting this error/if I'm using the web3 JavaScript API incorrectly?

Best Answer

Thank you to Gawey in the comments for the answer. The problem was that I used the wrong ABI to create the contract object while in the JavaScript console. To get the correct ABI, I went into the file Truffle created of the compiled contract and copied the ABI from there. To get the incorrect ABI, I had used the Truffle console and typed contractName.ABI, which gave me all of the contract's functions, however, that ABI didn't specify each function's inputs and outputs, which resulted in the TypeError.

Related Topic