How to Read multiple value from as JSON format from a external service and store in composer registry

0 votes

I am trying to read multiple value from as JSON format from a external service and store in composer registry. However it is getting error as below:

Error: Expected a Resource or Concept.

Please find attached my code below and help me understand.

concept TempReadings {
    o DateTime[] redDate
    o String[] minTemp
    o String[] maxTemp
    o String[] avgTemp
}
asset Temperature identified by uniqueId {
    o String uniqueId
    o TempReadings redTemp
}

transaction GetTempDetails {
    o String orderId
    o String prodId
    o String secureBoxId

}

event CreateTempDetailsEvent {
    o String uniqueId
}

async function GetTempDetails(uID){
    var factory = getFactory();
    var NS_M = 'org.acme.securebox';
    var uniqueID = uID.orderId + "-" + uID.prodId + "-" + uID.secureBoxId;
    var temperature = factory.newResource(NS_M, 'Temperature', uniqueID);
    const  temp = await request.get({ uri:  'https://<MYURL>/  $filter= ORDERID 
    eq "uniqueID" ', json: true });

    temperature.uniqueId = uniqueID;
    for(i=0;i<temp.d.results.length;i++){
        temperature.redTemp.redDate[i] = temp.d.results[i].date;
        temperature.redTemp.minTemp[i] = temp.d.results[i].tempMin;
        temperature.redTemp.maxTemp[i] = temp.d.results[i].tempMax;
        temperature.redTemp.avgTemp[i] = temp.d.results[i].tempAvg;

    }
    // save the order
    return getAssetRegistry(temperature.getFullyQualifiedType())
       .then(function (registry) {
           return registry.add(temperature);
       })
       .then(function(){
           var CreateTempDetailsEvent = factory.newEvent(NS_M, 
           'CreateTempDetailsEvent');
           CreateTempDetailsEvent.uniqueId = temperature.uniqueId;
           emit(CreateTempDetailsEvent);
       });
}

Sep 19, 2018 in Blockchain by slayer
• 29,050 points
36 views

1 answer to this question.

0 votes

Hey buddy, 

this works:

/** 
 * Track temperatures 
 * @param {org.acme.securebox.GetTempDetails} GetTempDetails - temperatures to be processed 
 * @transaction 
 */ 

async function GetTempDetails(uID){ 

var factory = getFactory(); 
var NS_M = 'org.acme.securebox'; 
var uniqueId = uID.orderId + "-" + uID.prodId + "-" + uID.secureBoxId; 

var temperature = factory.newResource(NS_M, 'Temperature', uniqueId); 
temperature.redTemp = []; 

const temp = await request.get({ uri: '<myURL>? $filter= ORDERID eq "uniqueId" ', json: true }); 

//this data below is a sample of what comes back from request response assigned to `temp` (I can't call your request URI :-)  - format your output if different

var temp = '{ "ID": "123", "ORDERID": "123", "date": "2018-01-01T00:00:00", "tempMin": "16", "tempMax": "24", "tempAvg": "20"}, { "ID": "456", "ORDERID": "456", "date": "2018-01-01T00:00:00", "tempMin": "16", "tempMax": "24","tempAvg": "20"} '; 

// stringify results 
var tempstr = JSON.stringify(temp); 

// create an array of objects 
var jsonobj = JSON.parse('[' + JSON.parse(tempstr) + ']'); 


for(i=0 ; i < jsonobj.length; i++ ) { 

    var redTemp = factory.newConcept(NS_M, 'TempReadings'); 

    console.log("ID field for this array element is " + jsonobj[i].ID); // all good 

    var str = jsonobj[i].date + 'Z';  // supplied date in `temp` isn't truly DateTime so...

    // convert the assumed string into an array. 
   //  eg    redTemp.redDate = [new Date('2011-04-11T10:20:30Z')]; 

    redTemp.redDate = [new Date(str)]; 
    redTemp.minTemp = [jsonobj[i].tempMin]; 

    redTemp.maxTemp = [jsonobj[i].tempMax]; 
    redTemp.avgTemp = [jsonobj[i].tempAvg]; 

    // temperature.redTemp = [redtemp]; 
    temperature.redTemp.push(redTemp); 

    console.log("temperature date for array redTemp element " + i + " is: " + temperature.redTemp[i].redDate); 

}  // end for 

// save the order to the ledger 

return getAssetRegistry(temperature.getFullyQualifiedType()) 
  .then(function (registry) { 
      return registry.addAll([temperature]); 
  }) 
  .then(function(){ 
      // var CreateTempDetailsEvent = factory.newEvent(NS_M, 'CreateTempDetailsEvent'); 
      // CreateTempDetailsEvent.uniqueId = temperature.uniqueId; 
      // emit(CreateTempDetailsEvent); 
       console.log("everything is done"); 
  }); 

} // end function 
answered Sep 19, 2018 by digger
• 27,630 points

Related Questions In Blockchain

+1 vote
2 answers

How to return value from a chaincode in Hyperledger Fabric?

Hyperledger Fabric supports only 2 types of ...READ MORE

answered Jun 13, 2018 in Blockchain by Perry
• 17,010 points
763 views
0 votes
1 answer

How to read the ETH value and other token values from an account?

You can do this eth.accounts shows you all known ...READ MORE

answered Oct 22, 2018 in Blockchain by Omkar
• 67,290 points
26 views
0 votes
1 answer

How to store picture(s) in a hyperledger blockchain channel

You can hold images as encrypted characters ...READ MORE

answered Jun 16, 2018 in Blockchain by charlie_brown
• 7,710 points
129 views
0 votes
1 answer

How to prevent the smart contract from being modified and deployed in the blockchain network?

To expand on Matthew's answer, each state ...READ MORE

answered Jul 5, 2018 in Blockchain by ariaholic
• 7,320 points
45 views
0 votes
1 answer
0 votes
1 answer

Invalid Batch or signature in Savtooth

This will solve your problem import org.apache.commons.codec.binary.Hex; Transaction txn ...READ MORE

answered Aug 1, 2018 in Blockchain by digger
• 27,630 points
58 views
+1 vote
1 answer
0 votes
1 answer

How to get all address and send ethers in solidity using a loop?

I found a similar code somewhere: contract  Holders{ uint ...READ MORE

answered Jul 31, 2018 in Blockchain by digger
• 27,630 points
144 views
0 votes
1 answer

How to use Proc/lamba returned from a method in Ruby?

You need to remove the colon: list.select(&valid_transaction) The & ...READ MORE

answered Aug 24, 2018 in Blockchain by digger
• 27,630 points
40 views