Hyperledger Fabric: How to run node js chaincode in dev mode?

+2 votes

I am trying to build my chaincode using the following command:

node main-chaincode.js --peer.address peer:7052

I get the following error message:

Error: Illegal value for namevalue element of type string: undefined (not a string) at fail
(/opt/gopath/src/chaincode/lavinias_chaincode/node/node_modules/protobufjs/dist/protobuf.js:1817:23) at
Element.ProtoBuf.Reflect.ElementPrototype.verifyValue
(/opt/gopath/src/chaincode/lavinias_chaincode/node/node_modules/protobufjs/dist/protobuf.js:1879:25) at
Field.ProtoBuf.Reflect.FieldPrototype.verifyValue
(/opt/gopath/src/chaincode/lavinias_chaincode/node/node_modules/protobufjs/dist/protobuf.js:3499:33) at
Message.setter
(/opt/gopath/src/chaincode/lavinias_chaincode/node/node_modules/protobufjs/dist/protobuf.js:2582:77) at
Function.start (/opt/gopath/src/chaincode/lavinias_chaincode/node/node_modules/fabric-shim/lib/chaincode.js:118:15) at start (/opt/gopath/src/chaincode/lavinias_chaincode/node/main-chaincode.js:46:8) at Object. (/opt/gopath/src/chaincode/lavinias_chaincode/node/main-chaincode.js:49:1) at
Module._compile (module.js:643:30) at Object.Module._extensions..js (module.js:654:10) at Module.load
(module.js:556:32):654:10) at Module.load (module.js:556:32)

Here is my chaincode:

    'use strict';
const shim = require('fabric-shim');
const Chaincode = class {
  async Init(stub) {
       let ret = stub.getFunctionAndParameters();
       let args = ret.params;
       if(args.length != 2) {
         return shim.error("Error. Invalid call arguments.")
       }
       await stub.putState(args[0], Buffer.from(args[1]));

       return shim.success('Initialized Successfully!');
   }

   async Invoke(stub) {
       let functionAndParams = stub.getFunctionAndParameters();
       if(functionAndParams.fcn === 'get') {
         return get(stub, functionAndParams.params);
       }
       else if(functionAndParams.fcn === 'set'){
         return set(stub, functionAndParams.params);
       }
       return shim.error("Error. Invalid invocation.");
   }
};

function get(stub, params) {
  if(params.length != 1) {
    return shim.error("Error. Invalid invocation call.");
  }
  return stub.getState(params[0]);
}

function set(stub, params) {
  if(params.length != 2) {
    return shim.error("Error. Invalid number of arguments.");
  }
  return stub.putState(params[0], params[1]);
}

var start = function start(){
  var cc = new Chaincode();
  shim.start(cc);
}

start();

Can someone help me?

Nov 2, 2018 in Blockchain by digger
• 27,620 points
530 views

3 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
+2 votes
Best answer

Hi, the command you are using to build is not right. Use this to build:

 CORE_CHAINCODE_ID_NAME="mycc:v0" node main-chaincode.js --peer.address peer:7052
answered Nov 2, 2018 by Omkar
• 66,880 points

selected May 6 by digger
+1 vote

To run the chaincode in dev mode, try this command:

$ peer chaincode install -l node -n mycc -p /opt/gopath/src/github.com/mycc -v v0
answered May 6 by Kriti
0 votes

I used the following command and it worked like a charm:

$ $(npm bin)/fabric-chaincode-node start --peer.address localhost:7052 --chaincode-id-name "mycontract:v0"
answered May 6 by Nati

Related Questions In Blockchain

0 votes
1 answer
+1 vote
2 answers

How to return value from a chaincode in Hyperledger Fabric?

Hyperledger Fabric supports only 2 types of ...READ MORE

answered Jun 13, 2018 in Blockchain by Perry
• 16,450 points
632 views
0 votes
1 answer

How to set chaincode path in Hyperledger Fabric?

For chaincode to properly run on your ...READ MORE

answered Jul 27, 2018 in Blockchain by digger
• 27,620 points
369 views
0 votes
0 answers
0 votes
1 answer
0 votes
1 answer

Docker: How to connect Node.js in container to Hyperledger Fabric?

You need to map ~/.composer/cards of your Docker host ...READ MORE

answered Oct 31, 2018 in Blockchain by Omkar
• 66,880 points
62 views
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.