wearetheledger / fabric-node-chaincode-utils

Utilities for writing and testing hyperledger Fabric nodejs chaincode
https://theledger.be
MIT License
78 stars 29 forks source link

Update Chaincode.ts #13

Closed CallForSanity closed 6 years ago

CallForSanity commented 6 years ago

In case 0 == payload the existing if-statement evaluates to true. A query then returns null which is unexpected behavior.

Please test my change request before merging. I have NOT tested it.

sneljo1 commented 6 years ago

I'm not sure in which case you would need this. Could you show an example or write a test for it?

CallForSanity commented 6 years ago

My code could be wrong, but consider this:

`export class Test extends Chaincode {

async init(stubHelper: StubHelper) {
    await stubHelper.putState('a', 0);
}
async getA(stubHelper: StubHelper, args: string[]): Promise<number> {
    return (await stubHelper.getStateAsObject('a')) as number;
}

}`

The problem is that the if-clause will evaluate the returned 0 from getA as true and the if-body then returns null. If your client code expects a number such as 0, this is a problem. In my case angular.io was simple not producing any output if the query return value was changed from 0 to null which was very confusing to debug.

A proper test for null should be performed instead. Maybe even https://nodejs.org/api/util.html#util_util_isnullorundefined_object