0

我无法从该getStateByPartialCompositeKey方法中获得响应。我可以使用putState并且getState很好,但不能使用getStateByPartialCompositeKey. 我正在使用 Fabric 链码的 NodeJS fabric-contract-api 版本。

对于开发,我使用了这个商业票据示例。这个例子使用了测试网络环境,它工作得很好。

这个实现提供了一些我用来与账本交互的账本 API 实用程序类。它有两个类,state.jsstateList.js在 stateliest 中添加了一个方法来调用getAllStatesFromPartialCompositeKeytry 和 usegetStateByPartialCompositeKey方法。

包.json

{
  "name": "informed-consent-form-contract",
  "version": "0.0.5",
  "description": "Papernet Contract",
  "main": "index.js",
  "engines": {
    "node": ">=8",
    "npm": ">=5"
  },
  "scripts": {
    "lint": "eslint .",
    "pretest": "npm run lint",
    "test": "nyc mocha test --recursive",
    "start": "fabric-chaincode-node start",
    "mocha": "mocha test --recursive"
  },
  "engineStrict": true,
  "author": "hyperledger",
  "license": "Apache-2.0",
  "dependencies": {
    "axios": "^0.19.2",
    "fabric-contract-api": "^2.0.0",
    "fabric-shim": "^2.0.0",
    "node-fetch": "^2.6.0",
    "nodemon": "^2.0.2"
  },
  "devDependencies": {
    "chai": "^4.1.2",
    "chai-as-promised": "^7.1.1",
    "eslint": "^4.19.1",
    "mocha": "^5.2.0",
    "nyc": "^12.0.2",
    "sinon": "^6.0.0",
    "sinon-chai": "^3.2.0"
  },
  "nyc": {
    "exclude": [
      "coverage/**",
      "test/**"
    ],
    "reporter": [
      "text-summary",
      "html"
    ],
    "all": true,
    "check-coverage": true,
    "statements": 100,
    "branches": 100,
    "functions": 100,
    "lines": 100
  }
}

Contract.js 片段:

/**
 * 
 * @param {*} ctx 
 * @param {*} partialKey - Piece of key to serch
 */
async getAllStates(ctx, partialKey) {
  let result = await ctx.informedConsentFormStateList.getStatesFromPartialCompositeKey(partialKey);
  console.log('================ Result inside getAllStates method ================');
  console.log(result);
  return result;
} // fin func

Insinde informedConsentFormStateList.js:

```
async getStatesFromPartialCompositeKey(partialKey) {
        return this.getAllStatesFromPartialCompositeKey(partialKey);
    }
```

修改后的 stateList.js 内部:

/**
* @function getAllStatesFromPartialCompositeKey
*/
async getAllStatesFromPartialCompositeKey(partialArgument) {
  console.log('================ Called from beginning of getAllStatesFromPartialCompositeKey ================');
  console.log('================ name => ' + this.name);
  let key = this.ctx.stub.createCompositeKey(this.name, [partialArgument]);
  console.log('================ created partial key .....');
  console.log(partialArgument);
  console.log(key);
  let response = await this.ctx.stub.getStateByPartialCompositeKey(this.name, [key]); //.toString('utf8')
  console.log('================ response below ================');
  console.log(response);
  let results = await this.getAllResults(response);
  return results;
} // fin getAllStatesFromPartialCompositeKey

这是我调用它的方式:

/home/ubilab/fabric2/fabric-samples/test-network$ peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name informed_consent_9 --tls true --cafile $ORDERER_CA --peerAddresses localhost:7051 --tlsRootCertFiles $ORG1_CA -c '{"Args":["csacmpcc:getAllStates","P03"]}'

回复:

chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200 payload:"[]"

链码容器日志:

================ Called from beginning of getAllStatesFromPartialCompositeKey ================
================ name => org.csa.informedconsentformstatelist
================ created partial key .....
P03
org.csa.informedconsentformstatelistP03
================ Result inside getAllStates method ================
[]

Couchdb 存储状态:

{
  "id": "\u0000org.csa.informedconsentformstatelist\u0000\"P03\"\u0000\"R68\"\u0000",
  "key": "\u0000org.csa.informedconsentformstatelist\u0000\"P03\"\u0000\"R68\"\u0000",
  "value": {
    "rev": "1-74db76a10ad8251ce2ba49ad58710ad8"
  },
  "doc": {
    "_id": "\u0000org.csa.informedconsentformstatelist\u0000\"P03\"\u0000\"R68\"\u0000",
    "_rev": "1-74db76a10ad8251ce2ba49ad58710ad8",
    "class": "org.csa.informedconsentform",
    "consentStatus": "1",
    "currentState": null,
    "key": "\"P03\":\"R68\"",
    "patientID": "P03",
    "reserachID": "R68",
    "sensors": "{numberOfSensors:1,sensors:[{sensorID:s01,name:SPO2,startDate:5/12/2020,endDate:5/12/2021}]}",
    "~version": "CgMBZQA="
  }
}

如果您需要更多信息来帮助我,请随时询问:)。谢谢!

4

2 回答 2

1

鉴于您使用单个属性进行查询,请尝试直接使用partialKey变量而不是构建复合键。

let response = await this.ctx.stub.getStateByPartialCompositeKey(this.name, [partialKey]);

API 文档

于 2020-05-21T18:12:08.350 回答
0

获得响应的更好方法是使用迭代器:

  let allResults = [];
  let res = { done: false, value: null };
  let jsonRes = {};
  res= await ctx.stub.getStateByPartialCompositeKey(this.name, [partialKey]);
    

  

while (!res.done) {
  jsonRes.Key = res.value.key;

  try {
      jsonRes.Record = JSON.parse(res.value.value.toString('utf8'));
      allResults.push(jsonRes);
      res = await iterator.next();
    }
   catch (err) {
      console.log(err);
      return {}
  }
  
}
await iterator.close();
return allResults;
于 2021-04-04T19:57:06.977 回答