首页 > 解决方案 > 我尝试使用链码查询该项目,但它显示 [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]

标签: javascriptjsonhyperledger-fabric

解决方案


推荐阅读