使用 web3 文档中的这个示例,当调用 async .then 时,如何获取原始传递的参数“123”?
myContract.methods.myMethod(123).call({来自: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'}) .then(函数(结果){
...
});
我可以更改合同回报以包含信息,但我似乎忽略了一些明显的事情。
如果您想访问 .then 回调函数中原始传递的参数“123”,那么您将需要捕获该值。这段代码可能对你有帮助。
const myValue = 123;
myContract.methods.myMethod(myValue).call({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'})
.then(function(result) {
console.log("myValue:", myValue); // here you can access the original passed parameter "123"
console.log("Result from contract:", result); // you can also access the result returned by the contract
})
.catch(function(error) {
console.error("Error:", error);
});
在此代码中,myValue 变量在 .then 回调的范围之外定义,并且 javascript 函数可以访问其外部范围内的变量,myValue 的值被捕获,您可以在 .then 回调中访问它