I want to query an asset on hyper ledger fabric blockchain. I have defined a function queryCar for it. Car is an asset on the blockchain with four/five different properties, like
model, color, etc.(I have copied the chaincode directly from the fabric sample GitHub repo) I'm using fabric-network along with fabric-ca-client for building my
SDK in nodejs. I am getting this error :
Failed to evaluate transaction: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received null
and this as response when I call the API for querycar
{
"result": "The \"path\" argument must be of type string or an instance of Buffer or URL. Received null",
"error": null,
"errorData": null
}
Node js API for query function:
app.get('/channels/:channelName/chaincodes/:chaincodeName', async function (req, res) {
try {
logger.debug('==================== QUERY BY CHAINCODE ==================');
var channelName = req.params.channelName;
var chaincodeName = req.params.chaincodeName;
console.log(`chaincode name is :${chaincodeName}`)
let args = req.query.args;
let fcn = req.query.fcn;
let peer = req.query.peer;
if (!chaincodeName) {
res.json(getErrorMessage('\'chaincodeName\''));
return;
}
if (!channelName) {
res.json(getErrorMessage('\'channelName\''));
return;
}
if (!fcn) {
res.json(getErrorMessage('\'fcn\''));
return;
}
if (!args) {
res.json(getErrorMessage('\'args\''));
return;
}
console.log('args==========', args);
args = args.replace(/'/g, '"');
args = JSON.parse(args);
logger.debug(args);
let message = await query.query(channelName, chaincodeName, args, fcn, req.username, req.orgname);
const response_payload = {
result: message,
error: null,
errorData: null
}
res.send(response_payload);
} catch (error) {
const response_payload = {
result: null,
error: error.name,
errorData: error.message
}
res.send(response_payload)
}
});
Helper query blockchain function
const query = async (channelName, chaincodeName, args, fcn, username, org_name) => {
try {
const ccp = await helper.getCCP(org_name) //JSON.parse(ccpJSON);
// Create a new file system based wallet for managing identities.
const walletPath = await helper.getWalletPath(org_name) //.join(process.cwd(), 'wallet');
const wallet = await Wallets.newFileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the user.
let identity = await wallet.get(username);
if (!identity) {
console.log(`An identity for the user ${username} does not exist in the wallet, so registering user`);
await helper.getRegisteredUser(username, org_name, true)
identity = await wallet.get(username);
console.log('Run the registerUser.js application before retrying');
return;
}
// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccp, {
wallet, identity: username, discovery: { enabled: true, asLocalhost: true }
});
// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork(channelName);
// Get the contract from the network.
const contract = network.getContract(chaincodeName);
let result;
switch (fcn) {
case "queryCar":
console.log("=============")
result = await contract.evaluateTransaction('SmartContract:'+fcn, args[0]);
break;
case "GetHistoryForAsset":
case "GetCarById":
console.log("=============")
result = await contract.evaluateTransaction('SmartContract:'+fcn, args[0]);
break;
default:
break;
}
console.log(result)
console.log(`Transaction has been evaluated, result is: ${result.toString()}`);
result = JSON.parse(result.toString());
return result
} catch (error) {
console.error(`Failed to evaluate transaction: ${error}`);
return error.message
}
}