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

Ssend a json with some empty values fail #5

Closed chrc closed 6 years ago

chrc commented 6 years ago

Hello,

I use @theledger/fabric-chaincode-utils to write my chaincode, and do stubHelper.putState(verifiedArgs.key, myObject).

I have a json object with a empty value, like: { name: 'HOME', firstname: 'John', foo: '', age: 30 } When I want to do channel.sendTransactionProposal() I got a error "Calling chaincode Invoke() returned error response [undefined]. Sending ERROR message back to peer"

How It is possible to put/send a json with some empty values?

Thanks by advance 🙂

sneljo1 commented 6 years ago

You should be perfectly able to do this. Can you provide your full code?

chrc commented 6 years ago
import {Chaincode, ChaincodeError, Helpers, StubHelper} from '@theledger/fabric-chaincode-utils';
import * as Yup from 'yup';

...

export class MyChaincode extends Chaincode {

  //args: [ 'KEY01', '', '' ]
  async postIntoFabric(stubHelper: StubHelper, args: string[]) {

    const verifiedArgs = await Helpers.checkArgs<any>(args, Yup.object()
        .shape({
            key: Yup.string().required(),
            title: Yup.string().notRequired(),
            name: Yup.string().nullable(true), 
        })
    );
...
}    

I got this error :

error: [MyChaincode] Error: ValidationError: title is a required field
    at /...../node/node_modules/@theledger/fabric-chaincode-utils/src/utils/helpers.ts:63:19
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
error: [MyChaincode] Data of error ValidationError: title is a required field: undefined
chrc commented 6 years ago

I'm trying to use:

 @types/yup: "^0.24.2",
 typescript: "^2.7.2"
jestersimpps commented 6 years ago

Did you send your args as a string array or as a json object? If so, can you log your args and paste a copy of the output? The verifiedArgs method will compose a js object based on the string array you send to the chaincode (['arg1','arg2','arg3',...], for example in this case, this could be sth like: ['key1', 'director', 'john'], and the Yup schema. The method will iterate over the keys in the yup schema and insert the values from the string array top down. Additionally, it will return an error if the rendered object doesn't match the Yup schema: https://github.com/wearetheledger/fabric-node-chaincode-utils/blob/master/src/utils/helpers.ts

chrc commented 6 years ago

Hi, With last versions it's ok now :) Thanks!

sneljo1 commented 6 years ago

There's still a bug with error handling, you shouldn't be seeing "[undefined]". I'll keep this issue open to track this.