Search code examples
node.jshttptrontronweb

TypeError: contract.test is not a function tronweb nodejs


I am trying to access Tron smart contract which is deployed on the Shasta test network using Tron-Web. I am using node-casiko server Following is my code:

const TronWeb = require('tronweb')

// This provider is optional, you can just use a url for the nodes instead
const HttpProvider = TronWeb.providers.HttpProvider;


const fullNode = 'https://api.shasta.trongrid.io';
const solidityNode = 'https://api.shasta.trongrid.io';
const eventServer = 'https://api.shasta.trongrid.io';
const privateKey = 'my test account private key';

const tronWeb = new TronWeb(
    fullNode,
    solidityNode,
    eventServer,
    privateKey
);


module.exports = {
        tmp: async function (req, res, next){
                try{
                        var contract = await tronWeb.trx.getContract("TS71i14jzLawbtu4qPDKEsFERSp6CQb93948");
                        //res.send(contract)
                        var result = await contract.test().call();
                        //res.send(result);
                }
                catch(err){
                        console.error(err)
                }
        }
}

When I execute this js file using node having version v10.15.3. I am getting Error as:

TypeError: contract.test is not a function at tmp (/home/administrator/node-Casiko/models/tronContract.js:25:32) at process._tickCallback (internal/process/next_tick.js:68:7)

If I print the contract variable it is printing all information about the contract, but it is not calling the contract methods. Can somebody help me with this problem?


Solution

  • Well, I managed to find an answer for self-question. Just change the code as below. It works!!

    module.exports = {
            tmp: async function (req, res, next){
                    try{
                            var contract = await tronWeb.contract()
                                       .at("TS71i14jzLawbtu4qPDKEsFERSp6CQb93948")
                            var result = await contract.test().call();
                    }
                    catch(err){
                            console.error(err)
                    }
            }