Xneon
Xneon

Reputation: 3

How to console log value from an asset? in hyperledger composer?

I am writing a network by hyperledger composer playground.

Here i have an asset called patient, and patient has list of hospitals, i have a transaction named GetPatientHospitals which is used to call the function getPatientHospitals, I want this function to print out a list of the ids (the thing like resource:org.acme.patientchain.PatientHospital#5wyjftthjr when i test), but when i test my function, it only told me my transaction has been submitted, no where i could see the output, is there any way to do it? or I need another asset to store these messages?

My getPatientHospitals function:

function getPatientHospitals(gethospitals){
    return getAssetRegistry('org.acme.patientchain.Patient')
         .then(function (PatientAssetRegistry) {
    // Get the patient asset
        return  PatientAssetRegistry.get(gethospitals.patient.pubKeyPatient);
     })
  .then(function (patienthospital) {
    return patienthospital.hospitals;
  })
} //list of hospitals

My GetPatientHospitals transaction and Patient asset:

transaction GetPatientHospitals { 
--> Patient patient
}



asset Patient identified by pubKeyPatient {
 o String pubKeyPatient 
--> PatientHospital[] hospitals

}

This is the hospitals in a patient I tested:

{
  "$class": "org.acme.patientchain.Patient",
  "pubKeyPatient": "1652",
  "hospitals": [
    "resource:org.acme.patientchain.PatientHospital#5wyjftthjr",
    "resource:org.acme.patientchain.PatientHospital#mgnl6ag4vh",
    "resource:org.acme.patientchain.PatientHospital#5wyjftthjr"
  ]
}

I want to print those recourses or just the id after the #

but there was no anywhere i could see the output, can I do "print" in this playground?

Upvotes: 0

Views: 224

Answers (2)

Riccardo
Riccardo

Reputation: 1173

You can use console.log() inside your js file. Then you can see the output in the browser developer console. For Firefox and Chrome you can show the developer console with CTRL-SHIFT-I. This works only if you are using Playground with a 'Web' profile, then you will see console.log output in the Browser Console. If you use Playground connected to a local Fabric instance, then the console.log output will be in the log of the Chaincode container.

Try with console.log(patienthospital.hospitals) and check the output in the developer console.

Upvotes: 1

Isha Padalia
Isha Padalia

Reputation: 1222

For print the value you need to make one transaction in cto file.

Hyperledger composer provides Transaction processor functions. It can optionally return data to client applications. This can be useful for returning a receipt to the submitter of the transaction or returning an asset modified by the transaction to avoid a separate lookup of the asset after the transaction has been committed. Data can also be returned to the client application via a transaction REST API for the business network.

I recommend you to follow the below links :

  1. returning data from transaction processor function
  2. composer github

Hope it will help you :)

Upvotes: 0

Related Questions