无法从 getStateByPartialCompositeKey 获得响应
Cannot get response from getStateByPartialCompositeKey
我无法从 getStateByPartialCompositeKey
方法获得响应。我可以使用 putState
和 getState
,但不能使用 getStateByPartialCompositeKey
。我正在使用 Fabric 链码的 NodeJS fabric-contract-api 版本。
为了开发,我使用了this commercial-paper example。本例使用test-network环境,运行正常
This implementation 提供了一些分类帐 API 实用程序 类,我也使用它来与分类帐进行交互。它有两个 类、state.js
和 stateList.js
我添加了一个名为 getAllStatesFromPartialCompositeKey
的方法来尝试使用 getStateByPartialCompositeKey
方法。
Package.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
内部 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="
}
}
如果您需要更多信息来帮助我,请随时询问 :)。谢谢!
鉴于您正在使用单个属性进行查询,请尝试直接使用 partialKey 变量而不是构建复合键。
let response = await this.ctx.stub.getStateByPartialCompositeKey(this.name, [partialKey]);
获得响应的更好方法是使用迭代器:
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;
我无法从 getStateByPartialCompositeKey
方法获得响应。我可以使用 putState
和 getState
,但不能使用 getStateByPartialCompositeKey
。我正在使用 Fabric 链码的 NodeJS fabric-contract-api 版本。
为了开发,我使用了this commercial-paper example。本例使用test-network环境,运行正常
This implementation 提供了一些分类帐 API 实用程序 类,我也使用它来与分类帐进行交互。它有两个 类、state.js
和 stateList.js
我添加了一个名为 getAllStatesFromPartialCompositeKey
的方法来尝试使用 getStateByPartialCompositeKey
方法。
Package.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
内部 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="
}
}
如果您需要更多信息来帮助我,请随时询问 :)。谢谢!
鉴于您正在使用单个属性进行查询,请尝试直接使用 partialKey 变量而不是构建复合键。
let response = await this.ctx.stub.getStateByPartialCompositeKey(this.name, [partialKey]);
获得响应的更好方法是使用迭代器:
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;