fabric-samples/fabcar/query.js
Jim Zhang ca8fad3151 [FAB-5260] Update samples to v1.0.0
basic-network:
 - moved "crypto-config" to top-level and removed
   "network" folder
 - added generate.sh to re-gen crypto materials
 - docker-compose.yaml to ref v1.0.0 images
 - add CLI to docker-compose
 - start.sh to only start CA, orderer, peer and couch
 - startFabric.sh to run start.sh, then launch CLI
   for create channel, install, instantiate, invoke
fabcar:
 - moved chaincode to central chaincode folder
 - moved "creds" to top-level and removed "network"
   folder
 - changed to use the basic-network as the target
   and removed docker-compose.yaml and crypto-config
 - updated package.json to require v1.0.0 modules

- added missing license headers
- restructured to use a central chaincode subdirectory

Change-Id: Ic784d1cf55ea51da5155624f3c38275883de1dca
Signed-off-by: Christopher Ferris <chrisfer@us.ibm.com>
Signed-off-by: Nick Gaski <ngaski@us.ibm.com>
Signed-off-by: Jim Zhang <jzhang@us.ibm.com>
2017-07-12 09:07:39 -04:00

68 lines
2.2 KiB
JavaScript

'use strict';
/*
* Copyright IBM Corp All Rights Reserved
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
* Hyperledger Fabric Sample Query Program
*/
var hfc = require('fabric-client');
var path = require('path');
var options = {
wallet_path: path.join(__dirname, './creds'),
user_id: 'PeerAdmin',
channel_id: 'mychannel',
chaincode_id: 'fabcar',
network_url: 'grpc://localhost:7051',
};
var channel = {};
var client = null;
Promise.resolve().then(() => {
console.log("Create a client and set the wallet location");
client = new hfc();
return hfc.newDefaultKeyValueStore({ path: options.wallet_path });
}).then((wallet) => {
console.log("Set wallet path, and associate user ", options.user_id, " with application");
client.setStateStore(wallet);
return client.getUserContext(options.user_id, true);
}).then((user) => {
console.log("Check user is enrolled, and set a query URL in the network");
if (user === undefined || user.isEnrolled() === false) {
console.error("User not defined, or not enrolled - error");
}
channel = client.newChannel(options.channel_id);
channel.addPeer(client.newPeer(options.network_url));
return;
}).then(() => {
console.log("Make query");
var transaction_id = client.newTransactionID();
console.log("Assigning transaction_id: ", transaction_id._transaction_id);
// queryCar - requires 1 argument, ex: args: ['CAR4'],
// queryAllCars - requires no arguments , ex: args: [''],
const request = {
chaincodeId: options.chaincode_id,
txId: transaction_id,
fcn: 'queryAllCars',
args: ['']
};
return channel.queryByChaincode(request);
}).then((query_responses) => {
console.log("returned from query");
if (!query_responses.length) {
console.log("No payloads were returned from query");
} else {
console.log("Query result count = ", query_responses.length)
}
if (query_responses[0] instanceof Error) {
console.error("error from query = ", query_responses[0]);
}
console.log("Response is ", query_responses[0].toString());
}).catch((err) => {
console.error("Caught Error", err);
});