Hyperledger架构:错误:chaincode参数错误:json:无法将数组解组为字符串类型的Go结构字段strArgs.Args [英] Hyperledger fabric: Error: chaincode argument error: json: cannot unmarshal array into Go struct field strArgs.Args of type string

查看:315
本文介绍了Hyperledger架构:错误:chaincode参数错误:json:无法将数组解组为字符串类型的Go结构字段strArgs.Args的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Hyperledger架构创建一个简单的数据库表.该表包含3个字段,ID,名称和出生日期.姓名和出生日期是字符串.我正在使用基本网络示例.这是我的密码:

I'm trying to create a simple database table using Hyperledger fabric. The table has 3 fields, ID, name and date of birth. Name and date of birth is string. I'm using the basic-network example. Here is my chaincode:

const shim = require('fabric-shim');
const util = require('util');

var Chaincode = class {

  // Initialize the chaincode
  async Init(stub) {
    console.info('========= example02 Init =========');
    console.log("HELLO WORLD!!!");
    console.log("Init: Does nothing!");
    return shim.success();
  }

  async Invoke(stub) {
    let ret = stub.getFunctionAndParameters();
    console.info("Truong: async Invoke!!");
    console.info(ret);
    let method = this[ret.fcn];
    if (!method) {
      console.log('no method of name:' + ret.fcn + ' found');
      return shim.success();
    }
    try {
      let payload = await method(stub, ret.params);
      return shim.success(payload);
    } catch (err) {
      console.log(err);
      return shim.error(err);
    }
  }

  // Insert
  async insert(stub, args) {
    console.log("Truong: async insert!!!");
    if (args.length != 2) {
      throw new Error('Incorrect number of arguments. Expecting 2');
    }

    let ID = args[0];
    let Attrs = args[1];
    await stub.putState(ID, Buffer.from(JSON.stringify(Attrs)));
  }

  // Delete
  async delete(stub, args) {
    console.info("Truong: async delete!!!");
    if (args.length != 1) {
      throw new Error('Incorrect number of arguments. Expecting 1');
    }

    let ID = args[0];

    // Delete the key from the state in ledger
    await stub.deleteState(ID);
  }

  // query callback representing the query of a chaincode
  async query(stub, args) {
    if (args.length != 1) {
      throw new Error('Incorrect number of arguments. Expecting name of the person to query')
    }

    let jsonResp = {};
    let ID = args[0];

    // Get the state from the ledger
    let Result = await stub.getState(A);
    if (!Result) {
      jsonResp.error = 'Failed to get state for ' + ID;
      throw new Error(JSON.stringify(jsonResp));
    }

    jsonResp.ID = ID;
    jsonResp.Attrs = Result.toString();
    console.info('Query Response:');
    console.info(jsonResp);
    return Result;
  }
};

shim.start(new Chaincode());

但是,当我键入以下行时:

However when I type the following line:

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/Admin@org1.example.com/msp" peer0.org1.example.com peer chaincode query -C mychannel -n mycc -c '{"Args":["insert", "1", {"name": "Dang Manh Truong", "date": "26/04/1995"}]}'

出现错误:

Error: chaincode argument error: json: cannot unmarshal array into Go struct field strArgs.Args of type string

这是否意味着Fabric不接受json作为输入?请帮助我,非常感谢.

Does it mean that fabric does not accept json as an input? Please help me, thank you very much.

推荐答案

Args必须是字符串数组.您将需要转义JSON内容:

Args must be an array of strings. You will need to escape the JSON content:

"{\"name\": \"Dang Manh Truong\", \"date\": \"26\/04\/1995\"}"

peer ... -c '{"Args":["insert", "1","{\"name\": \"Dang Manh Truong\", \"date\": \"26\/04\/1995\"}" ]}'

这篇关于Hyperledger架构:错误:chaincode参数错误:json:无法将数组解组为字符串类型的Go结构字段strArgs.Args的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆