admin管理员组

文章数量:1314320

I'm trying to call a simple contract method that just returns a string of data. I've based my code on the example that can be found in the docs => /reference#methodcall

tronWeb.trx.getContract("TFWbGYFVjUMKrHALdU4MnFWNYY9Uc5W9SZ").then(async contract => {
    console.log(contract);
    let abi = contract.abi;
    console.log(abi);
    let c = await tronWeb.contract({
        abi
    });
    let result = await c.getBadgeOwner('something is up').call();
    console.log(result);
});

The difference with what can be found in the docs, is that I'm loading the abi from my loaded contract, instead of hard coding it like in the example.

The error I get is index.js:105 Uncaught (in promise) TypeError: e.forEach is not a function which seems to refer to the abi somehow:

I'm trying to call a simple contract method that just returns a string of data. I've based my code on the example that can be found in the docs => https://developers.tronwork/reference#methodcall

tronWeb.trx.getContract("TFWbGYFVjUMKrHALdU4MnFWNYY9Uc5W9SZ").then(async contract => {
    console.log(contract);
    let abi = contract.abi;
    console.log(abi);
    let c = await tronWeb.contract({
        abi
    });
    let result = await c.getBadgeOwner('something is up').call();
    console.log(result);
});

The difference with what can be found in the docs, is that I'm loading the abi from my loaded contract, instead of hard coding it like in the example.

The error I get is index.js:105 Uncaught (in promise) TypeError: e.forEach is not a function which seems to refer to the abi somehow:

Share Improve this question edited Feb 20, 2023 at 20:11 TylerH 21.1k77 gold badges79 silver badges112 bronze badges asked Jan 30, 2019 at 8:51 JorreJorre 17.6k33 gold badges103 silver badges147 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

For anyone tripping over the same beginner mistake, here's how to solve it:

Use contract().at() instead of getContract()

let contract = await tronWeb
        .contract()
        .at("TFWbGYFVjUMKrHALdU4MnFWNYY9Uc5W9SZ")

After that, you can call your contract methods just fine

let currentValue = await contract.getBadgeOwner('something is up').call();
setTimeout(async () => {
   this.myContractOb = await 
   this.tronWeb.contract(myContract).at(this.contractAddress);
},10000);

Using above code with myContract as ABI json object having same issue.

I was doing the same mistake before. This works for me

async function a (){
        let contract = await tronWeb.contract().at("TFWbGYFVjUMKrHALdU4MnFWNYY9Uc5W9SZ")
        //console.log(contract);
        let currentValue = await contract.getBadgeOwner('something is up').call();
        console.log(currentValue);
}

a()

本文标签: javascriptHow to call a contract method with tronwebStack Overflow