1

I developped a hyperldger app using the fabcar logic using node.
Everything works fine when submitting transactions.
I created a rest API server with express to interact with the chaincode and use an angular app to serve the rest api.
While the createCertificate transaction works well, the api request about adding a certificate to the ledger throws the arror above.
The addCertificate function from the smart contact

async createCertificate(ctx, certificateNumber, studentFullName, apogee, cin, birthDate, birthPlace, title, honor, graduationDate) {
        console.info('============= START : Create Certificate ===========');

        const certificate = {
            studentFullName,
            docType: 'certificate',
            apogee,
            cin,
            birthDate,
            birthPlace,
            title,
            honor,
            graduationDate
        };

        await ctx.stub.putState(certificateNumber, Buffer.from(JSON.stringify(certificate)));
        console.info('============= END : Create Certificate ===========');
    }

The request api for adding cetificate

app.post('/api/addcertificate/', urlencodedParser, async function (req, res) {
    try {
const ccpPath = path.resolve(__dirname, '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json');
        const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));
// Create a new file system based wallet for managing identities.
        const walletPath = path.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.
        const identity = await wallet.get('appUser');
        if (!identity) {
            console.log('An identity for the user "appUser" does not exist in the wallet');
            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: 'appUser', discovery: { enabled: true, asLocalhost: true } });

        // Get the network (channel) our contract is deployed to.
        const network = await gateway.getNetwork('mychannel');

        // Get the contract from the network.
        const contract = network.getContract('certificate');
// Submit the specified transaction.
        // createCertificate transaction - requires 8 argument, ex: ('createCertificate', 'CERT12', 'Honda', 'Accord', 'Black', 'Tom')
        // changeCarOwner transaction - requires 2 args , ex: ('changeStudentName', 'CERT10', 'HADI')
        await contract.submitTransaction('createCertificate', req.body.certificateNumber, req.body.studentFullName, req.body.apogee, req.body.cin, req.body.birthDate, req.body.birthPlace, 
        req.body.title, req.body.honor, req.body.graduationDate);
        console.log('Transaction has been submitted');
        res.send('Transaction has been submitted');
// Disconnect from the gateway.
        await gateway.disconnect();
} catch (error) {
        console.error(`Failed to submit transaction: ${error}`);
        process.exit(1);
    }
})

The error :

Failed to submit transaction: TypeError: Cannot read property 'toString' of undefined

Any hint, clarification and/or how to get express logs is appreciated.

comocoder
  • 84
  • 6

2 Answers2

0

Beating the bush 1: I found out that one of the other API in the file was throwing this issue.

require('crypto').randomBytes(48, function(err, buffer) {
var token = buffer.toString('hex');
});

the toString in the abaove code snippet was giving an issue. Worked after removing it.

Beating the bush 2:

req = {"body": {"a":1, "b":2}}
req.a ==> wrong
data = req.body = {"a":1, "b":2}
data.a => correct
Hem M
  • 326
  • 2
  • 13
0

Make sure sure your are passing the values through submitTransaction function call in a string format. For example:

await contract.submitTransaction('createCertificate', `${ req.body.certificateNumber }`, `${ req.body.studentFullName }`, `${ req.body.apogee }`, `${ req.body.cin }`, `${ req.body.birthDate }`, `${ req.body.birthPlace }`, `${ req.body.title }`, `${ req.body.honor }`, `${ req.body.graduationDate}` );
M_Yeasin
  • 46
  • 4