gpt4 book ai didi

node.js - Hyperledger fabric : Error: chaincode argument error: json: cannot unmarshal array into Go struct field strArgs. 字符串类型的参数

转载 作者:行者123 更新时间:2023-12-05 03:03:21 25 4
gpt4 key购买 nike

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

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());

但是当我输入以下行时:

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 作为输入?请帮助我,非常感谢。

最佳答案

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

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

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

关于node.js - Hyperledger fabric : Error: chaincode argument error: json: cannot unmarshal array into Go struct field strArgs. 字符串类型的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54157356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com