Reputation: 11
I am getting this type of error Cannot read property 'address' of undefined while i am calling sendTRX function
{
const privateKey = "***";
var fromAddress = window.tronWeb.address.toHex("TR18f2revPbnb9dzFTqYQ5TKaBjX1QEtsC"); //address _from
var toAddress = window.tronWeb.address.toHex("TC4AKrNsrhG3h1pnY7SjjNcqQpDNhmNTuY"); //address _to
var amount = 10000000; //amount
//Creates an unsigned TRX transfer transaction
console.log(fromAddress);
//console.log(window.tronWeb.address.toHex(fromAddress));
const tradeobj = await window.tronWeb.transactionBuilder.sendTrx(
fromAddress,
amount,
toAddress
);
const signedtxn = await window.tronWeb.trx.sign(
tradeobj,
privateKey
);
const receipt = await window.tronWeb.trx.sendRawTransaction(
signedtxn
);
console.log('- Output:', receipt, '\n');
}
sendTRX();```
Upvotes: 1
Views: 2073
Reputation: 11
I was facing the same issue I resolved it by putting the file into htdocs (XAMPP) folder and accessing it by HTTP://localhost/trontest.html ,So I think window.tronWeb is only accessible in HTTP or HTTPS So you can use XAMPP or Node or any server.
Here is my code for trontest.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button onclick="gettronweb()">Can you get tronweb from tronlink?</button>
<script>
function gettronweb(){
if(window.tronWeb && window.tronWeb.defaultAddress.base58){
document.write("Yes, catch it:",window.tronWeb.defaultAddress.base58)
}
}
</script>
</body>
</html>
Upvotes: 1
Reputation: 1
this is tronlink chrome extension related. When you call window object , make sure you are logged in on your tronlink wallet.
Without tronlink make sure to define user ports with function:
window.onload = function() {
if (!window.tronWeb) {
const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider('https://api.trongrid.io');
const solidityNode = new HttpProvider('https://api.trongrid.io');
const eventServer = 'https://api.trongrid.io/';
const tronWeb = new TronWeb(
fullNode,
solidityNode,
eventServer,
);
window.tronWeb = tronWeb;
}
};
Upvotes: 0