1

async createEntity(ctx, entityNumber, entityType, data) {
  const entity = {
    data,
  };
  await ctx.stub.putState(entityType + entityNumber, Buffer.from(JSON.stringify(entity)));
  return ctx.stub.getTxID();
}

There is such code in chaincode, in response I get for example

612d6a6b5919fbc511e7a7b691cd349eb932f4e8d84ab9394885d3220f2e169a

And having written down some information there, the question is, how do I get this data back using the received txId?

Staler FBM
  • 38
  • 7
  • ctx.stub.GetState and ctx.stub.PutState these function write state to levelDB's. if you want to access your data by txID one of workaround which i have used is store data against that txID. ie – Block Crasher Apr 08 '20 at 13:55

3 Answers3

0

With getState, as you could have expected: https://hyperledger.github.io/fabric-chaincode-node/release-2.0/api/fabric-shim.ChaincodeStub.html#getState__anchor.

kekomal
  • 2,179
  • 11
  • 12
  • Are you sure? I try to add an object, and after receiving the hash, I try to find it, in response I get nothing – Staler FBM Apr 06 '20 at 11:22
  • Are you sure your previous transaction has been committed? Maybe you have endorsed it, but not committed. https://hyperledger-fabric.readthedocs.io/en/release-2.0/txflow.html – kekomal Apr 06 '20 at 12:08
0

A workaround would be to compare the transaction id you get as return value of ctx.stub.getTxID() with all the transaction ids you get using getHistoryForKey(entityType + entityNumber). And the match in txnid is obviously your desired data (state).

But this is still a cumbersome process. Please post if you find a direct way to get data using just the txnId. Would be a treat if such any exists.

0

ctx.stub.GetState and ctx.stub.PutState these function write state to levelDB's. if you want to access your data by txID one of workaround which i have used is to store data against that txID. ie

let txID =  ctx.stub.getTxID();
await ctx.stub.putState(txID, Buffer.from(JSON.stringify(entity)));
Block Crasher
  • 304
  • 2
  • 6