简体   繁体   中英

HyperLedger Composer: Tranaction error TypeError: getAssetRegistry(…).get is not a function

I'm trying to re-create one of my chaincode written in GoLang in Composer. Model.cto

asset Carton identified by cartonId {
o String cartonId
o String manufacturerId
o String dateOfDeparture optional
o String recipient optional 
o String currentOwner
o String status
--> Unit[] units optional
o Trackrecord[] trackrecord optional 
}
 transaction MakeCarton {
 --> Unit[] unit
 o String Id
 }
 asset Unit identified by unitId {
 o String unitId
 o String cartonId
 o String manufacturerId
 o String dateOfDeparture
 o String recipient
 o Trackrecord[] trackrecord
 o String currentOwner
 o String status  
  }

So I need to create transaction which creates one carton and accepts array of units and the cartonId. Every carton contains some units and units are also assets , so i need to update their cartonIds in the unit assets in worldstate.

  function makeCarton(make) {
   var carton = getFactory().newResource('org.acme.mynetwork','Carton',make.Id);
   //carton.cartonId = make.Id ;
    var unitobjs = new Array() ;
    for(var i=0; i < make.unit.length ; i++) {
    var unitobj = getFactory().newResource('org.acme.mynetwork','Unit',make.unit[i].unitId) ;
    unitobj.cartonId = make.Id ;     
    unitobj = getFactory().newRelationship('org.acme.mynetwork','Unit',make.unit[i].unitId) ;
    unitobjs.push(unitobj) ;
     }

     carton.units = unitobjs ;

    // getFactory().newRelationship('org.acme.mynetwork','Unit',make.unit[i].unitId)
    for(var i=0; i < make.unit.length ; i++) {  
    carton.manufacturerId= make.unit[i].manufacturerId ;
     carton.currentOwner = make.unit[i].currentOwner ;
     carton.status = 'At '+ make.unit[i].currentOwner ;
      }

      return getAssetRegistry('org.acme.mynetwork.Carton').then(function (assetRegistry) {
     // Update the asset in the asset registry.
      return assetRegistry.add(carton).then(function() {
      for(var i=0; i < make.unit.length ; i++) {
       return getAssetRegistry('org.acme.mynetwork.Unit').then(function (unitRegistry) {

         // var unitobj = getFactory().newResource('org.acme.mynetwork','Unit',make.unit[i].unitId) ;
           var unitobj = getAssetRegistry('org.acme.mynetwork.Unit').get(make.unit[i].unitId);
           //unitRegistry.get(make.unit[i].unitId);
     unitobj.cartonId = make.Id ;      
      return unitRegistry.update(unitobj);
    })
      //.then(function(unitobj){
    //unitobj.cartonId = make.Id;
    //untiRegistry.update(unitobj);});
     }
});
});
 }

Submitting this transaction generates an TypeError: getAssetRegistry(...).get is not a function

The line var unitobj = getAssetRegistry('org.acme.mynetwork.Unit').get(make.unit[i].unitId); returns a Promise to an asset registry, so you need a .then like you have for your other calls to getAssetRegistry().

This might help: https://github.com/hyperledger/composer-sample-networks/blob/master/packages/perishable-network/lib/logic.js#L89

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM