0

How can I enter input data value in HTML Text Box?

enter image description here

The output is good in console.log, but the HTML Text Box displays the error Object Promise.

enter image description here

I want to know how to print the value of web3.eth.getTransactionFromBlock(blocknumber).input into the Html Text box. Please help me.

These are the source code I have tried many times, but failed.

var transaction = web3.eth.getTransactionFromBlock(1865, 0).then(function (tx) {
        console.log(tx.input)
    });

tto_count.value = transaction;
web3.eth.getCoinbase(
    function (a, coinbase) {
    a = (async() => {
        await web3.eth.getTransactionFromBlock(1688, 0, console.log)
    })()
    document.getElementById("transaction").append("transaction input data  : " + web3.eth.getTransactionFromBlock(1688, 0) + "\n");
    cb = coinbase;
});

0

Promises are asynchronous so the flow of the code may not be in the same sequence as the lines of code.

var transaction = web3.eth.getTransactionFromBlock(1865, 0).then(function (tx) {
        console.log(tx.input)   // This executes later
    });

tto_count.value = transaction;    //This executes first

The result of web3.eth.getTransactionFromBlock is available in the then block. The variable transaction will just hold the Promise returned.

Using async & await you can make it seem sequential in way.

web3.eth.getCoinbase(async function(a, coinbase) {
    a = await web3.eth.getTransactionFromBlock(4461, 0);

    document.getElementById("transaction").append("transaction input data  : " + a.input + "\n");
    cb = coinbase;        
});

This code will run in the same sequence as it is written. This will update your HTML as you intended to. If you're new to Javascript Promises and async-await, I would highly recommended reading about it first.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.