Reputation: 119
Probabilly i'm missing some concepts of callbacks and promises here, but i can't find the way to my code works.
Here is it:
var tx;
web3.eth.getBlock(6339515, function(err, result){
for(var i = 0; i <= result.transactions.length; i++){
tx = result.transactions[i];
getInputTransaction(tx)
.then(function() {} )
.catch(function(error) {
console.log('error: \n' + error);
});
}
})
async function getInputTransaction(tx) {
web3.eth.getTransaction(tx, function(err, cb){
console.log('got here');
let decodeInput = web3.utils.hexToAscii(cb.input);
decodeInput = decodeInput.split("_").pop();
if(!err){
console.log(cb);
console.log('\nInput decoded: ' + '\u001b[1;32m' + decodeInput + '\u001b[0m');
}else{
console.log('error: ' + error);
}}
)
}
Basically, i want get the result callback of first method to get each index value in pass it to the second method to scan this value, in this case is an ethereum transaction to get the input value. The problem is the callback named "cb" isn't triggered.
The respectives documentations:
What am i missing here?
Upvotes: 1
Views: 214
Reputation: 119
Got it.
The problem was i wasn't ensure the parameter cb
in getInputTransaction
With if(cb && cb.input != undefined)
before let decodeInput = web3.utils.hexToAscii(cb.input)
it works well.
Upvotes: 0
Reputation: 83418
I do not know answer outright why it is not triggered. But some tips. Use the new await
syntax when calling web3 functions, so you do not need to write callbacks and the code is linear and easier to analyse.
Use TypeScript instead of JavaScript, as TypeScript compiler might not let you to compile a code that leads errorneus situations like this.
Also, I think this line might be missing return
:
web3.eth.getTransaction(tx, function(err, cb)
Upvotes: 1