javascript - 我尝试使用链码查询该项目,但它显示 [object object]。我不知道我使用了错误的输入或锁链错误
问题描述
我尝试通过asset_tranfer_ledger_chaincode.js
用作参考来创建链码。并使用丰富的查询和 REST api。
这是我的链码
async CreateItem(ctx , plantCode, plantName, productionDate, productCode, productName, lotNo, rmInfors, premixInfors , packageInfors , process , owner , transactionDate, email) {
const exists = await this.ItemExists(ctx, productCode);
if(exists) {
throw new Error(`The item ${productCode} already exists`);
}
let item = {
docType: 'item',
plantCode: plantCode,
plantName: plantName,
productionDate: productionDate,
productCode: productCode,
productName: productName,
lotNo: lotNo,
rmInfors: rmInfors,
premixInfors: premixInfors,
packageInfors: packageInfors,
process: process,
owner: owner,
transactionDate: transactionDate,
email: email
};
await ctx.stub.putState(productCode, Buffer.from(JSON.stringify(item)));
let indexName = 'Code~name';
let codeNameIndexKey = await ctx.stub.createCompositeKey(indexName, [item.productCode, item.productName]);
await ctx.stub.putState(codeNameIndexKey, Buffer.from('\u0000'));
}
// query with couchdb index -------------
async QueryItems(ctx, queryString) {
return await this.GetQueryResultForQueryString(ctx, queryString);
}
async GetQueryResultForQueryString(ctx, queryString) {
let resultsIterator = await ctx.stub.getQueryResult(queryString);
let results = await this._GetAllResults(resultsIterator, false);
return JSON.stringify(results);
}
async _GetAllResults(iterator, isHistory) {
let allResults = [];
let res = await iterator.next();
while (!res.done) {
if (res.value && res.value.value.toString()) {
let jsonRes = {};
console.log(res.value.value.toString('utf8'));
if (isHistory && isHistory === true) {
jsonRes.TxId = res.value.tx_id;
jsonRes.Timestamp = res.value.timestamp;
try {
jsonRes.Value = JSON.parse(res.value.value.toString('utf8'));
} catch (err) {
console.log(err);
jsonRes.Value = res.value.value.toString('utf8');
}
} else {
jsonRes.Key = res.value.key;
try {
jsonRes.Record = JSON.parse(res.value.value.toString('utf8'));
} catch (err) {
console.log(err);
jsonRes.Record = res.value.value.toString('utf8');
}
}
allResults.push(jsonRes);
}
res = await iterator.next();
}
iterator.close();
return allResults;
}
在我调用 CreateItem 之后
{
"plantCode": "test",
"plantName": "test",
"productionDate": "2021-03-25",
"productCode": "test",
"productName": "test",
"lotNo": "test",
"rmInfors": [
{
"code": "test",
"name": "test",
"lotNo": "test"
}
],
"premixInfors": [
{
"code": "test",
"name": "test",
"lotNo": "test"
},
{
"code": "test",
"name": "test",
"lotNo": "test"
},
{
"code": "test",
"name": "test",
"lotNo": "test"
},
{
"code": "test",
"name": "test",
"lotNo": "test"
}
],
"packageInfors": [
{
"code": "test",
"name": "test",
"lotNo": "test"
},
{
"code": "test",
"name": "test",
"lotNo": "test"
},
{
"code": "test",
"name": "test",
"lotNo": "test"
}
],
"process": "test",
"owner": "test",
"transactionDate": "2021-04-23",
"email": "test@test.com"
}
然后使用带有 Args 的 Queryitems 进行查询
{"Args":["{\"selector\":{\"docType\":\"item\",\"productCode\":\"test\"}, \"use_index\":[\"_design/indexOwnerDoc\", \"indexOwner\"]}"]}
结果随之而来
{
docType: 'item',
email: 'test@test.com',
lotNo: 'test',
owner: 'test',
packageInfors: '[object Object],[object Object],[object Object]',
plantCode: '4086',
plantName: 'test',
premixInfors: '[object Object],[object Object],[object Object],[object Object]',
process: 'test',
productCode: 'test',
productName: 'test',
productionDate: '2021-03-25',
rmInfors: '"[object Object]"',
transactionDate: '2021-04-23'
}
结果出来了 [object Object]
我试图弄清楚,但我仍然是 javascript 和 Hyperledger-Fabric 的新手,我尝试搜索相关答案但没有看到正确的答案。
我希望查询结果没有 [object object]
解决方案
推荐阅读
- typescript - 错误:在“tsconfig”或“jsconfig”中设置“experimentalDecorators”选项以删除此警告
- php - 如何在 yii2 gridview 中根据条件隐藏列?
- azure-devops - 在 azure devops 中使用 azure powershell 内联任务获取 azure 功能键并将其放入 keyvault 的脚本
- java - Intellij 将私有字段及其访问器方法移动/重构到不同的类
- node.js - 无法保存 ids 数组 mongo
- elasticsearch - 密集向量数据类型的 Elasticseach 错误,值为空
- c# - 在命令提示符下工作的命令在 C# 中不起作用
- python - 如何通过读取文本文件中提到的日期来计算总天数?
- mysql - 如何从两个表中获取空值 WHERE user_id IS NULL 在 Node.js MYSQL 中不起作用
- python - Django:与多对多关系相关的问题