Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
.then() function never running for call to blockchain

Please find below my code snippet. Here credentialHash is not being set in the .then() call. Neither the "In then" or "Callback rejected" are being printed so I know that it is not running but I cant understand why. I've also included code for the setHash function in my smart contract.

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

The result returned from console.log(this.state.contract.methods.setHash(hash).send({from: this.state.account})) is :

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
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try dropping the .then() syntax entirely and use only the async/await style. What does the code below give?

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);
about 4 years ago · Juan Pablo Isaza Relatório

0

For anyone else with this issue, I finally found the answer. The default number of transactionConfirmationBlocks is 24 which takes a long time to complete. Setting this to 1 as below solved my issue.

this.setState({contract: new web3.eth.Contract(
      credentialArtifact.abi,
      deployedNetworkCred.address,
        {transactionConfirmationBlocks: 1}
  )});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda