Go-Ethereum

如何讓 HTML 文本框列印 console.log 的值?

  • June 17, 2020

如何在 HTML 文本框中輸入輸入數據值?

在此處輸入圖像描述

輸出很好console.log,但 HTML 文本框顯示錯誤Object Promise

在此處輸入圖像描述

我想知道如何將 的值列印web3.eth.getTransactionFromBlock(blocknumber).input到 Html 文本框中。請幫我。

這些是我嘗試過很多次但失敗的原始碼。

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;
});

Promise 是非同步的,因此程式碼流可能與程式碼行的順序不同。

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

tto_count.value = transaction;    //This executes first

的結果web3.eth.getTransactionFromBlock在 then 塊中可用。變數 transaction 將只保存返回的 Promise。

使用 async & await 你可以讓它看起來是順序的。

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;        
});

此程式碼將按照與編寫時相同的順序執行。這將按照您的意圖更新您的 HTML。如果您不熟悉 Javascript Promises 和 async-await,我強烈建議您先閱讀它。

引用自:https://ethereum.stackexchange.com/questions/84338