.then() 函数从不 运行 调用区块链
.then() function never running for call to blockchain
请在下面找到我的代码片段。这里 credentialHash 没有在 .then() 调用中设置。 “In then”或“Callback rejected”都没有被打印出来,所以我知道它不是 运行 但我不明白为什么。我还在我的智能合约中包含了 setHash 函数的代码。
let hash;
await ipfs.add(JSON.stringify(this.state.credential)).then((result, error) => {
hash = result.path;
if (error) {
console.error(error)
return;
}
// Store hash on blockchain
console.log("Hash before BC:" + hash);
this.state.contract.methods.setHash(hash).send({from: this.state.account}).then((resolve) => {
console.log("In then")
this.setState({credentialHash: hash});
}, (rejected) => {
console.log("Callback rejected");
});
console.log("After setting:" + this.state.credentialHash);
});
function setHash(string memory _CredentialHash) public returns(string memory) { credentialHash = _CredentialHash; return _CredentialHash; }
从console.log(this.state.contract.methods.setHash(hash).send({from: this.state.account}))
返回的结果是:
Proxy {promise: Promise, eventEmitter: EventEmitter, resolve: ƒ, reject: ƒ}
[[Handler]]: Object
get: ƒ proxyHandler(target, name)
[[Prototype]]: Object
[[Target]]: PromiEvent
eventEmitter: EventEmitter
_events: Events
[[Prototype]]: Object
_eventsCount: 0
[[Prototype]]: Object
promise: Promise
[[Prototype]]: Promise
[[PromiseState]]: "pending"
[[PromiseResult]]: undefined
reject: ƒ ()
resolve: ƒ ()
[[Prototype]]: Object
[[IsRevoked]]: false
尝试完全删除 .then()
语法并仅使用 async/await
样式。下面的代码给出了什么?
let hash, result;
try {
result = await ipfs.add(JSON.stringify(this.state.credential))
hash = result.path;
} catch (error) {
console.error(error)
return;
}
// Store hash on blockchain
console.log("Hash before BC:" + hash);
try {
await this.state.contract.methods.setHash(hash).send({
from: this.state.account
})
} catch(error) {
console.log("Callback rejected");
return
}
this.setState({
credentialHash: hash
});
console.log("After setting:" + this.state.credentialHash);
对于遇到此问题的其他人,我终于找到了答案。 transactionConfirmationBlocks 的默认数量是 24,这需要很长时间才能完成。如下所示将其设置为 1 解决了我的问题。
this.setState({contract: new web3.eth.Contract(
credentialArtifact.abi,
deployedNetworkCred.address,
{transactionConfirmationBlocks: 1}
)});
请在下面找到我的代码片段。这里 credentialHash 没有在 .then() 调用中设置。 “In then”或“Callback rejected”都没有被打印出来,所以我知道它不是 运行 但我不明白为什么。我还在我的智能合约中包含了 setHash 函数的代码。
let hash;
await ipfs.add(JSON.stringify(this.state.credential)).then((result, error) => {
hash = result.path;
if (error) {
console.error(error)
return;
}
// Store hash on blockchain
console.log("Hash before BC:" + hash);
this.state.contract.methods.setHash(hash).send({from: this.state.account}).then((resolve) => {
console.log("In then")
this.setState({credentialHash: hash});
}, (rejected) => {
console.log("Callback rejected");
});
console.log("After setting:" + this.state.credentialHash);
});
function setHash(string memory _CredentialHash) public returns(string memory) { credentialHash = _CredentialHash; return _CredentialHash; }
从console.log(this.state.contract.methods.setHash(hash).send({from: this.state.account}))
返回的结果是:
Proxy {promise: Promise, eventEmitter: EventEmitter, resolve: ƒ, reject: ƒ}
[[Handler]]: Object
get: ƒ proxyHandler(target, name)
[[Prototype]]: Object
[[Target]]: PromiEvent
eventEmitter: EventEmitter
_events: Events
[[Prototype]]: Object
_eventsCount: 0
[[Prototype]]: Object
promise: Promise
[[Prototype]]: Promise
[[PromiseState]]: "pending"
[[PromiseResult]]: undefined
reject: ƒ ()
resolve: ƒ ()
[[Prototype]]: Object
[[IsRevoked]]: false
尝试完全删除 .then()
语法并仅使用 async/await
样式。下面的代码给出了什么?
let hash, result;
try {
result = await ipfs.add(JSON.stringify(this.state.credential))
hash = result.path;
} catch (error) {
console.error(error)
return;
}
// Store hash on blockchain
console.log("Hash before BC:" + hash);
try {
await this.state.contract.methods.setHash(hash).send({
from: this.state.account
})
} catch(error) {
console.log("Callback rejected");
return
}
this.setState({
credentialHash: hash
});
console.log("After setting:" + this.state.credentialHash);
对于遇到此问题的其他人,我终于找到了答案。 transactionConfirmationBlocks 的默认数量是 24,这需要很长时间才能完成。如下所示将其设置为 1 解决了我的问题。
this.setState({contract: new web3.eth.Contract(
credentialArtifact.abi,
deployedNetworkCred.address,
{transactionConfirmationBlocks: 1}
)});