javascript—我尝试使用链码查询该项,但它显示了[object]我不知道我是否使用了错误的输入,或者使用了错误的链接

vktxenjb  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(214)

我尝试使用 asset_tranfer_ledger_chaincode.js 作为参考。并使用富查询和RESTAPI。
这是我的密码

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;
    }

在我用

{
    "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'
}

我已经尝试过了,但是我对javascript和hyperledger结构还是比较陌生,我尝试过搜索相关的答案,但没有找到正确的答案。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题