1

I have an object array like below

[ {"name":"heamoglobin","reading":"12"},
  {"name":"mrc","reading":"3.3"},
  {"name":"hct","reading":"33"} ]

I need to send this as an argument for my chaincode function. I tried stringifying the whole array like this

"[{\"name\":\"heamoglobin\",\"reading\":\"12\"},{\"name\":\"mrc\",\"reading\":\"3.3\"},{\"name\":\"hct\",\"reading\":\"33\"}]"

but didnt get a successful transaction

Any suggestions?

lp_nave
  • 244
  • 3
  • 17
  • For node.js did you try using JSON.stringify function? – Chintan Rajvir Apr 23 '20 at 04:19
  • yes but i used it only for the object array though "await contract.submitTransaction( "createReport", uid, req.body.patientID, user[0].email, clinicProfile.centerName, date, JSON.stringify(data) )" – lp_nave Apr 23 '20 at 04:35
  • I feel the arguments would be passed as strings. There are 6 arguments. So you pass function name + 6 strings. I assume uid, patientID and date are not strings. So that might be the issue. You must then parse correctly these arguments in the chaincode. – Chintan Rajvir Apr 23 '20 at 05:12
  • @ChintanRajvir I tried sending the whole object as one argument to the chaincode like this await contract.submitTransaction( "createReport", JSON.stringify( uid, req.body.patientID, user[0].email, clinicProfile.centerName, date, data ) ) didnt work – lp_nave Apr 23 '20 at 06:24
  • That is not the right way. You must convert arguments to strings separately like @kekomal shows in his answer below. – Chintan Rajvir Apr 23 '20 at 07:27

1 Answers1

3

You must convert to string each parameter that is not already string. Something like:

await contract.submitTransaction("createReport", uid, req.body.patientID, user[0].email, clinicProfile.centerName, date.toString(), JSON.stringify(data));

And then process suitably every parameter in your chaincode's operation (unmarshal the array, etc.).

kekomal
  • 2,179
  • 11
  • 12