1

I have modified fabcar example to store my own data. I have to save json data into the blocks. Some of the object properties are strings and array. But i can't able to pass the array to the chaincode.

Here is my json data

{
key : '5e57b8dbb9b30e3575f45d75',
Thp_stRpPmp: '0',
Thp_stSprPmp: '0',
Thp_stArrFans: [0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0],
Thp_bSpry: 'false',
}

While trying to submit transaction i am facing error like this.

error inside await Error: Transaction arguments must be strings:     

How to resolve this?

1 Answers1

1

As the error says, arguments can only be strings.

You can send your arguments marshaled as an string and unmarshal them in your chaincode.

When working with complex data, I usually send an only JSON marshaled string that I unmarshal in my chaincode. You lose in performance with un/marshaling process, but you win in maintainability when suitably matched to your program models.

kekomal
  • 2,179
  • 11
  • 12
  • If I marshal [false,false] to string, it will be converted to 'false','false', which is taken two parameters while passing to chain code. How to pass it as a single value – divya sekaran Apr 17 '20 at 07:13
  • I don't know (and cannot guess) how are you marshaling. I even don't know what SDK (programming language?) are you using. Are you using Javascript's `JSON.stringify()` or what? – kekomal Apr 17 '20 at 09:31
  • Yes i am using javascript SDK. Using JSON.stringify() – divya sekaran Apr 17 '20 at 10:27
  • `let response = await contract.submitTransaction('my_operation', JSON.stringify([false,false]));` should send it as an only string. – kekomal Apr 17 '20 at 10:59
  • @kekomal Hi! i have the same issue but im sending an object array along with other variables how do i do it then? (using node sdk) – lp_nave Apr 23 '20 at 03:40
  • @PamudithaNavaratne, you can use one parameter per variable (one for the marshaled object array and one for each one of the other variables) or embed all the variables and the array in a larger marshaled JSON object. As long as your chaincode operation is preparred to unmarshal the received parameters and your code is clean and maintainable, both options are valid. It is usually better to define models in your chaincode to unmarshal received data than unmarshaling to generic maps or slices. – kekomal Apr 23 '20 at 06:27