Reputation: 656
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/[email protected]/msp" peer0.org1.example.com peer chaincode query -C mychannel -n mycc -c '{"Args":["insert", "1", {"name": "Dang Manh Truong", "date": "26/04/1995"}]}'
There was an error:
Error: chaincode argument error: json: cannot unmarshal array into Go struct field strArgs.Args of type string
Does it mean that fabric does not accept json as an input? Please help me, thank you very much.
Upvotes: 3
Views: 2111
Reputation: 12013
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\"}" ]}'
Upvotes: 4