Skip to content

Instantly share code, notes, and snippets.

@MXmaster2s
Last active June 15, 2020 10:22
Show Gist options
  • Save MXmaster2s/ed7713a20eaadc8b08a53d33e4cda79a to your computer and use it in GitHub Desktop.
Save MXmaster2s/ed7713a20eaadc8b08a53d33e4cda79a to your computer and use it in GitHub Desktop.
ERROR while initiating the getStudent on client application: Unexpected end og JSON input
'use strict';
/**
* This is a Node.JS application to fetch a Student Account from network
* Defaults:
* StudentID: 0001
*/
const helper = require('./contractHelper');
async function main(studentId) {
try {
const certnetContract = await helper.getContractInstance();
console.log('.....Get Student Account');
const studentBuffer = await certnetContract.submitTransaction('getStudent', studentId);
// process response
console.log('.....Processing Get Student Transaction Response\n\n');
let existingStudent = JSON.parse(studentBuffer.toString());
console.log(existingStudent);
console.log('.....Get Student Transaction Complete!');
return existingStudent;
} catch (error) {
console.log(`\n\n ${error} \n\n`);
throw new Error(error);
} finally {
// Disconnect from the fabric gateway
helper.disconnect();
}
}
main('200').then(() => {
console.log('.....API Execution Complete!');
});
const fs = require('fs');
const yaml = require('js-yaml');
const { FileSystemWallet, Gateway } = require('fabric-network');
let gateway;
async function getContractInstance() {
// A gateway defines which peer is used to access Fabric network
// It uses a common connection profile (CCP) to connect to a Fabric Peer
// A CCP is defined manually in file connection-profile-mhrd.yaml
gateway = new Gateway();
// A wallet is where the credentials to be used for this transaction exist
// Credentials for user MHRD_ADMIN was initially added to this wallet.
const wallet = new FileSystemWallet('./identity/mhrd');
// What is the username of this Client user accessing the network?
const fabricUserName = 'MHRD_ADMIN';
// Load connection profile; will be used to locate a gateway; The CCP is converted from YAML to JSON.
let connectionProfile = yaml.safeLoad(fs.readFileSync('./connection-profile-mhrd.yaml', 'utf8'));
// Set connection options; identity and wallet
let connectionOptions = {
wallet: wallet,
identity: fabricUserName,
discovery: { enabled: false, asLocalhost: true }
};
// Connect to gateway using specified parameters
console.log('.....Connecting to Fabric Gateway');
await gateway.connect(connectionProfile, connectionOptions);
// Access certification channel
console.log('.....Connecting to channel - certificationchannel');
const channel = await gateway.getNetwork('certificationchannel');
// Get instance of deployed Certnet contract
// @param Name of chaincode
// @param Name of smart contract
console.log('.....Connecting to Certnet Smart Contract');
return channel.getContract('certnet', 'org.certification-network.certnet');
}
function disconnect() {
console.log('.....Disconnecting from Fabric Gateway');
gateway.disconnect();
}
module.exports.getContractInstance = getContractInstance;
module.exports.disconnect = disconnect;
/usr/bin/node --inspect-brk=19354 application/3_getStudent.js
Debugger listening on ws://127.0.0.1:19354/35350cd8-b5a3-45b6-b382-f52484949d86
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
.....Connecting to Fabric Gateway contractHelper.js:32
Error: EISDIR: illegal operation on a directory, read 3_getStudent.js:28
.....Disconnecting from Fabric Gateway contractHelper.js:47
(node:15603) UnhandledPromiseRejectionWarning: Error: Error: EISDIR: illegal operation on a directory, read
at main (/home/rohit/workspace/certification-network/application/3_getStudent.js:29:11) warning.js:25
(node:15603) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
warning.js:25
(node:15603) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
async getStudent(ctx, studentId, name) {
// Create the composite key required to fetch record from blockchain
const studentKey = ctx.stub.createCompositeKey('org.certification-network.certnet.student', [studentId]);
// Return value of student account from blockchain
let studentBuffer = await ctx.stub
.getState(studentKey)
.catch(err => console.log(err));
let studentbject = JSON.parse(studentBuffer.toString());
if(studentbject.name === name){
return studentbject
}else{
return console.log('Incorrect Student Name. Please Try Again!');
}
}
rohit@rohit-Swift-SF514-52T:~/workspace/certification-network/application$ node 3_getStudent.js
.....Connecting to Fabric Gateway
.....Connecting to channel - certificationchannel
.....Connecting to Certnet Smart Contract
.....Get Student Account
.....Processing Get Student Transaction Response
SyntaxError: Unexpected end of JSON input
.....Disconnecting from Fabric Gateway
(node:16184) UnhandledPromiseRejectionWarning: Error: SyntaxError: Unexpected end of JSON input
at main (/home/rohit/workspace/certification-network/application/3_getStudent.js:29:11)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:16184) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:16184) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
@MXmaster2s
Copy link
Author

MXmaster2s commented Jun 15, 2020

I am getting an error "Unexpected end of JSON input"

On further performing debugging, the code breaks on line 33 of contractHelper.js (await gateway.connect(connectionProfile, connectionOptions)

It seems like there is some problem in either accessing the wallet or reading the connection-profile-mhrd.yaml file.

Earlier the error during debugging was "unable to access connection-profile-mhrd.yaml" (for some reason the terminal was looking for the file in the main certification-network folder and not the application folder. The problem got resolved somehow (maybe?) and this is the new error "Error: EISDIR: illegal operation on a directory"

Basically IDK what is happening! 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment